我有window.onbeforeunload正确触发.它显示一个确认框,以确保用户知道他们正在导航(关闭)窗口,并且任何未保存的工作都将被删除.
我有一个独特的情况,如果用户通过单击链接导航离开页面,我不希望这触发,但我无法弄清楚如何检测是否已在函数内单击链接以停止该功能.这就是我对代码的看法:
window.onbeforeunload = function() {
var message = 'You are leaving the page.';
/* If this is Firefox */
if(/Firefox[\/\s](\d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
if(confirm(message)) {
history.go();
}
else {
window.setTimeout(function() {
window.stop();
}, 1);
}
}
/* Everything else */
else {
return message;
}
}
Run Code Online (Sandbox Code Playgroud) javascript ×1