使用onbeforeunload绑定事件

Bind an event with onbeforeunload

本文关键字:事件 绑定 onbeforeunload 使用      更新时间:2024-06-26

如果用户关闭页面,我使用以下代码提示消息:

window.onbeforeunload = function(e) {
    e = e ? e : window.event;
    if(e) { 
        e.returnValue = ''; 
    }
    return '';
}

当用户单击页面上的特殊链接(比如id="myid")时,我希望避免此消息显示的提示。有可能吗?我试过这样:

var source = e.target || e.srcElement;
console.log(source);

但是source就是null,是否可以绑定事件并用onbeforeunload进行检查?

"e"属性是什么?我该如何观察这个"物体"?知道吗?

如果单击链接,则取消绑定事件,然后重定向

window.onbeforeunload = null;