简体   繁体   English

beforeunload回调中的Javascript代码在Safari中不起作用

[英]Javascript code inside beforeunload callback not working in Safari

I have added code to change web page color on beforeunload event: 我添加了代码来更改beforeunload事件上的网页颜色:

window.onbeforeunload = function (){
    $('.navbar-collapse').css({background: 'blue'});
    console.log("Hello unload");
    return null;
  }
  • This code works properly on Chrome and Firefox but it doesn't work on Safari browser. 此代码可在Chrome和Firefox上正常运行,但不适用于Safari浏览器。
  • However, when I debug this code in Safari, when stepping over each line of code in the callback function, id DOES changes background color. 但是,当我在Safari中调试此代码时,在跳过回调函数中的每一行代码时,id确实会更改背景颜色。
  • When I navigate by clicking on an url on page and then click back button on browser, the background color is changed. 当我通过单击页面上的url进行导航,然后在浏览器上单击“后退”按钮时,背景颜色会更改。

Any idea? 任何想法?

window.onbeforenload is "magic" in the sense that it is special and more and more browsers are restricting what it can do. window.onbeforenload具有特殊意义,并且越来越多的浏览器限制了它的功能,因此它是“神奇的”。

Some browsers will let you display a custom message, other browers are free to ignore this event entirely. 一些浏览器将允许您显示自定义消息,其他浏览器则可以完全忽略此事件。

In short: Don't try to do anything but return a null or non-null value and hope for the best. 简而言之:除了返回null值或非null值并希望获得最佳结果外,不要尝试做任何事情。 It is out of your hands. 它不在您手中。

You might want to see this answer too: window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera? 您可能也想看看这个答案: window.onbeforeunload和window.onunload在Firefox,Safari,Opera中不起作用?

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

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