0

我需要在用户单击浏览器中的 [x] 时进行一些操作,而不是在页面因刷新或页面之间的正常导航而卸载时...

显然我不能依赖 onclose、onbeforeunload 或 onunload 事件,因为它们在导航到其他页面或刷新同一页面时也会被触发,而这不是必须完成这些操作的时刻......

我怎样才能做到这一点?

4

1 回答 1

0

我不知道这是否适用于 IE 以外的其他浏览器(规范规定它必须仅在 IE 中检查......)但这有效:

window.onbeforeunload = function() {
    if ((window.event.clientX < 0) || (window.event.clientY < 0) || (window.event.clientX < -80)) { // close button 
        //things to do when the user closes the tab or window pressing the [x]
    } else if (window.event.altKey == true) { // ALT + F4
        //things to do when the user closes with ALT+F4
    } 
};

好吧,如果用户单击书签,这将不起作用,因为它也是一个 clientY < 0 ...

于 2013-02-26T13:57:46.187 回答