使用 onbeforeunload 绑定事件

Bind an event with onbeforeunload

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

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

我想避免当用户单击页面上的特殊 link(比方说 id="myid")时显示此消息的提示。可能吗?我这样试过:

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

但是sourcenull,是否可以绑定事件并使用onbeforeunload进行检查?

"e" 属性是什么?我如何查看这个 "object" ?有什么想法吗?

如果点击 link 则取消绑定事件然后重定向

window.onbeforeunload = null;