如何使用JS / jquery确认何时离开页面 [英] How to use JS/jquery to confirm when navigating away from page
本文介绍了如何使用JS / jquery确认何时离开页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在用户试图离开时给出警告确认对话框...我有以下代码,但它不能正常工作......
jQuery(窗口).unload(function(){
var yes = confirm(你即将结束你的会话,你确定吗?);
if(yes){
return true;
} else {
return false;
}
});
弹出窗口很好但是当我点击否时,它仍会导航...... / p>
感谢您寻找......
解决方案
不需要JQuery:
window.onbeforeunload = function(){
return你即将结束你的会话,你确定吗? ?;
}
演示: http://jsfiddle.net/AlienWebguy/4fNCh/
I am trying to give an alert confirm dialog box when users try to navigate away...I have the following code but it doesn't quite work...
jQuery(window).unload(function() {
var yes = confirm("You're about to end your session, are you sure?");
if (yes) {
return true;
} else {
return false;
}
});
The popup comes up fine but when I click "NO", it still navigates away...
Thanks for looking...
解决方案
No need for JQuery:
window.onbeforeunload = function() {
return "You're about to end your session, are you sure?";
}
Demo: http://jsfiddle.net/AlienWebguy/4fNCh/
这篇关于如何使用JS / jquery确认何时离开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文