admin管理员组文章数量:1384332
I need to do remind the users something when they are leaving the page, and that can be handled with the window.onUnload event. But I also need to check if the user is navigating away by submitting the form on the page, or by clicking the navigation links. I could use the form's onSubmit event to set a flag, and then check against that flag in the window.onUnload event, but I am not sure which one fires first.
any ideas ?
I need to do remind the users something when they are leaving the page, and that can be handled with the window.onUnload event. But I also need to check if the user is navigating away by submitting the form on the page, or by clicking the navigation links. I could use the form's onSubmit event to set a flag, and then check against that flag in the window.onUnload event, but I am not sure which one fires first.
any ideas ?
Share Improve this question asked May 8, 2009 at 13:26 disc0dancerdisc0dancer 9,37511 gold badges40 silver badges46 bronze badges 1- 3 Why don't you do a test and see what fires first? – Svante Svenson Commented May 8, 2009 at 13:37
3 Answers
Reset to default 5You actually want window.onbeforeunload
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = 'Are You Sure?';
}
// For Safari
return 'Are You Sure?';
};
It turns out that the form.onSubmit event fires first so i can use a flag. I have checked this in Firefox and Safari only.
var isRefresh = true;
window.onunload = function () {
alert('the page was ' + (isRefresh == false ? 'NOT ' : '') + 'refreshed');
}
$('a').live('click', function () { isRefresh = false; alert('a link was clicked'); });
$('form').bind('submit', function () { isRefresh = false; alert('form was submitted'); });
Based on How to capture the browser window close event?. I added the refresh logic.
版权声明:本文标题:Javascript: Check if the user is navigating away with a form submit or a simple link click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744490405a2608698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论