简体   繁体   English

如何检测IE11关闭事件

[英]How to detect IE11 close event

I am now trying to detect only the event of the browser close button of IE11 in JavaScript.我现在正在尝试仅检测 JavaScript 中 IE11 浏览器关闭按钮的事件。

I was able to detect the browser close button with the onbeforeunload event, but I also have trouble picking up other page transition events.我能够使用 onbeforeunload 事件检测浏览器关闭按钮,但我也无法选择其他页面转换事件。

This code is I trying on it.这段代码是我正在尝试的。

window.onbeforeunload = function(event) {
    alert('Sure you want to close ?');
}

Is there any good way to do it ?有什么好的方法可以做到吗?

Try using the addEventListener syntax:尝试使用addEventListener语法:

window.addEventListener('beforeunload', function(){
    alert('Sure you want to close ?');
});

Actually the onbeforeunload Event will be fired when you close the browser/tab , refresh the page(or F5), submit a form, or jump to other pages..etc AFAIK, there is not a perfect way to detect which operation exactly USER do.But for your question, i guess there are some html tag like实际上onbeforeunload 事件将在您关闭浏览器/选项卡、刷新页面(或 F5)、提交表单或跳转到其他页面时触发。 .但是对于你的问题,我想有一些 html 标签,比如

<a href="abc.com">To ABC</a>

that fired the onbeforeunload Event when they are clicked.单击它们时触发onbeforeunload 事件 If it is true, may be you can remove the Event when the HREF was active so that the Event would not be fired.如果是这样,您可能可以在 HREF 处于活动状态时删除该事件,以便不会触发该事件。 Try codes blow:尝试代码吹:

<script type="text/javascript">
  $(function() {
      $("a").click(function() {
        window.onbeforeunload = null;
        location.href = $(this).attr("href");
        return false;
      });
  });
  window.onbeforeunload = function(e) {
     return 'Sure you want to close ?';
  }
</script>

Note that this would not prevent the refresh opreation to fired the onbeforeunload Event , but i hope to provide some ideas for resolving your problem.请注意,这不会阻止触发onbeforeunload 事件的刷新操作,但我希望为解决您的问题提供一些想法。

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

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