简体   繁体   English

当用户在浏览器窗口中按X时,我们可以捕获事件吗?

[英]Can we capture an event when user presses X on the browser window?

is there a way differentiate between refresh and close in Javascript? 有没有办法区分JavaScript中的刷新和关闭? i use window.onbeforeunload now which works great in IE. 我现在使用window.onbeforeunload,在IE中效果很好。 To void this being called anywhere but close, i set a flag on EVERY button ...this seems like a difficult, hacky solution, but after searching for hours on google, it was the best i could find. 为了使这个在除关闭位置之外的任何位置都无效,我在每个按钮上都设置了一个标志...这似乎是一个困难,棘手的解决方案,但是在Google上搜索了数小时之后,这是我能找到的最好的方法。

Isn't there an onclose event for IE????? IE上没有onclose事件吗???? or onclick? 或onclick?

I only need to support IE, not FF, so please don't post anything FF related, I can't use it. 我只需要支持IE,不支持FF,所以请不要发布任何与FF相关的内容,我不能使用它。 Thanks! 谢谢!

As far as I know there is no way to trap in JavaScript the click of the X (close) button in any browser. 据我所知,没有办法在JavaScript中捕获任何浏览器中X(关闭)按钮的单击。 In my opinion this is due to security concern... JavaScript can't (an will never be able to) listen to OS messages like the close of the current window. 在我看来,这是出于安全方面的考虑……JavaScript无法(永远无法)监听OS消息,例如当前窗口的关闭。

The only solution I know (which is what you seem to use) is: 我知道的唯一解决方案(这似乎是您使用的)是:

<body onunload="alert('Fired');">

But as you find out if fires on close and on reload. 但是当您发现近距离射击并重新加载时会射击。 Again this is normal behavior because, AFAIK, for the JS engine a reload is the same as a close then a reopen of the same page. 再次,这是正常现象,因为AFAIK,对于JS引擎,重新加载与关闭然后重新打开同一页面相同。

AFAIK,不,没有IE特定的onclose事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM