Is there a cross-browser onload event when clicking the back button?

后端 未结 15 2342
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 22:46

For all major browsers (except IE), the JavaScript onload event doesn’t fire when the page loads as a result of a back button operation — it only fires when the

15条回答
  •  梦毁少年i
    2020-11-21 23:18

    Guys, I found that JQuery has only one effect: the page is reloaded when the back button is pressed. This has nothing to do with "ready".

    How does this work? Well, JQuery adds an onunload event listener.

    // http://code.jquery.com/jquery-latest.js
    jQuery(window).bind("unload", function() { // ...
    

    By default, it does nothing. But somehow this seems to trigger a reload in Safari, Opera and Mozilla -- no matter what the event handler contains.

    [edit(Nickolay): here's why it works that way: webkit.org, developer.mozilla.org. Please read those articles (or my summary in a separate answer below) and consider whether you really need to do this and make your page load slower for your users.]

    Can't believe it? Try this:

    
    
    click me, then press the back button
    
    

    You will see similar results when using JQuery.

    You may want to compare to this one without onunload

    
    
    click me, then press the back button
    
    

提交回复
热议问题