【问题标题】:Onbeforeunload not working on Chrome/Firefox but works in IEOnbeforeunload 在 Chrome/Firefox 上不工作,但在 IE 上工作
【发布时间】:2018-08-01 23:00:00
【问题描述】:

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

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

此代码可能出现什么问题。 :( 我无法理解。我希望它在所有浏览器中都能正常工作,以便在页面存在之前向用户显示确认弹出窗口。

【问题讨论】:

  • 你应该提到你想通过这个活动实现什么..

标签: javascript google-chrome internet-explorer firefox


【解决方案1】:

在卸载处理之前由于网页表现不佳而被修改。因此,浏览器可能会或可能不会在卸载事件处理期间抑制警报。

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

以下示例在主流浏览器和 IE 中运行(至少适用于 windows 10)。

  • Firefox 和 IE 报告了event.returnValue 中提供的消息。
  • Chrome 忽略了该消息并简单地询问

    离开网站?
    您所做的更改可能不会被保存。

  • 只有 IE 显示警告框。

var unloadFunction = function( event){
    event.returnValue = "do you really want to leave this page";
    alert("unloading");
};
window.addEventListener('beforeunload', unloadFunction); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    相关资源
    最近更新 更多