0

I have added code to change web page color on beforeunload event:

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.
  • However, when I debug this code in Safari, when stepping over each line of code in the callback function, id DOES changes background color.
  • When I navigate by clicking on an url on page and then click back button on browser, the background color is changed.

Any idea?

D Nguyen
  • 129
  • 2
  • 4

1 Answers1

0

window.onbeforenload is "magic" in the sense that it is special and more and more browsers are restricting what it can do.

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. 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?

Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74