Onbeforeunload无法在Chrome

Onbeforeunload无法在Chrome

本文介绍了Onbeforeunload无法在Chrome/Firefox上运行,但可以在IE中运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为onbeforeunload编写了代码.它适用于IE,但不适用于任何其他浏览器.

I wrote a code for onbeforeunload. It works for IE but not for any other browser.

var unloadFunction = function(){return "";}
window.addEventListener('beforeunload', unloadFunction);

此代码可能会出错. :(我听不懂.我希望它能在所有浏览器中正常工作,以便在一个页面存在之前向用户显示确认弹出窗口.

What can possibly go wrong with this code. :( I am not able to understand. I want this to work in all browsers in order to show a confirm popup to the user before one exist the page.

推荐答案

由于网页表现不佳,自从引入卸载处理以来,已经对其进行了修改.结果,浏览器在卸载事件处理期间可能会抑制警报,也可能不会抑制警报.

Before unload processing has been modified since it was introduced due to badly behaved web pages. In result, browsers may or may not suppress alerts during unload event processing.

有关详细信息,请参考 MDN ,但请注意提到的returnValue是事件对象的属性,而不是事件处理程序函数返回的值.

Refer to MDN for detailed information, but note the returnValue mentioned is a property of the event object, not a value returned from the event handler function.

下面的示例在主要的浏览器和IE中运行(至少适用于Windows 10).

The example below runs in major browsers and IE (for windows 10 at least).

  • Firefox和IE报告了event.returnValue中提供的消息.
  • Chrome忽略了该消息,只是询问了一下
  • Firefox and IE reported the message provided in event.returnValue.
  • Chrome ignored the message and simply asked

  • 只有IE显示了警报框.
  • var unloadFunction = function( event){
        event.returnValue = "do you really want to leave this page";
        alert("unloading");
    };
    window.addEventListener('beforeunload', unloadFunction); 

    这篇关于Onbeforeunload无法在Chrome/Firefox上运行,但可以在IE中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-24 13:56