我正在尝试如下所示的 onbeforeunload 事件,当我关闭浏览器时它工作正常
<script type="text/javascript" language="javascript">
window.onbeforeunload = LeavingSiteHandler;
function LeavingSiteHandler(e) {
if (!e)
e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
</script>
但是,当我按 F5 刷新页面或单击内部链接时,将触发此事件。是否可以检查点击的超链接是否为内部链接?如果我按 F5,是否可以停止此弹出消息? 非常感谢!
最佳答案
您可以为页面中的链接编写一个 onclick
处理程序,在其中删除 window.onbeforeunload
处理程序。
但是您无法区分页面刷新和关闭选项卡/窗口。
关于javascript - 离开站点 javascript 事件 - onbeforeunload - 刷新页面/内部超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5501163/