admin管理员组文章数量:1244220
To stop the user from leaving my page, I'm using this javascript:
window.onbeforeunload = function()
{
if($('textarea#usermessage').val() != '')
{
return "You have started writing a message.";
}
};
This shows a message warning the user not to leave the page because they've started writing a message but not submitted. This shows up to save the user from losing this data.
Now, how can I stop this message from showing up when the form is submitted?
To stop the user from leaving my page, I'm using this javascript:
window.onbeforeunload = function()
{
if($('textarea#usermessage').val() != '')
{
return "You have started writing a message.";
}
};
This shows a message warning the user not to leave the page because they've started writing a message but not submitted. This shows up to save the user from losing this data.
Now, how can I stop this message from showing up when the form is submitted?
Share Improve this question edited Jul 8, 2011 at 8:27 Paul D. Waite 98.8k57 gold badges202 silver badges271 bronze badges asked Jul 8, 2011 at 8:18 ingh.amingh.am 26.8k43 gold badges132 silver badges179 bronze badges2 Answers
Reset to default 9function handleWindowLeaving(message, form) {
$(window).bind('beforeunload', function (event) {
return message;
});
$(form).bind('submit', function () {
$(window).unbind('beforeunload');
});
};
when calling this, pass your message and the form reference, and it will automatically remove onbeforeunload when submitting the form. Otherwise, show message to user
Set a flag in your form's submit handler; check whether the flag is set in your unload handler. If it is - voila, it's a form submit!
本文标签: javascriptStop user from leaving web pageexcept for when submitting formStack Overflow
版权声明:本文标题:javascript - Stop user from leaving web page, except for when submitting form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740176670a2236551.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论