【发布时间】:2013-02-23 00:26:03
【问题描述】:
我有 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;
}
}
【问题讨论】:
标签: javascript