Maison  >  Article  >  interface Web  >  JavaScript 捕获窗口关闭事件_javascript技巧

JavaScript 捕获窗口关闭事件_javascript技巧

WBOY
WBOYoriginal
2016-05-16 18:49:081075parcourir
1.用javascript重新定义 window.onbeforeunload() 事件
在javascript里定义一个函数即可
function window.onbeforeunload() { alert("关闭窗口")}
alert()事件将会在关闭窗口前执行,你也可以用户决定是否关闭窗口
function window.onbeforeunload() {
if (event.clientX>document.body.clientWidth && event.clientYwindow.event.returnValue="确定要退出本页吗?";
}
2.用onUnload方法
在body 标签里加入onUnload事件
body onUnload="myClose()"
然后在javascript里定义myClose()方法
但是onUnload方法是在关闭窗口之后执行,不是在关闭窗口之前执行,如果你想在关闭窗口之前做判断,请用第一种方法

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn