Page reload in Chrome unnecessarily triggers bound events just prior to reloading the page

Tags: , , , ,



Hope someone has a good answer to this:

Why does Chrome (14.0) triggers the document ready and window load events when I refresh the page? Note that I am not talking about what happens when the new page loads, but before it has loaded. See the following code:

<form name="form1" method="post" action="tmp.aspx?a=1" id="form1">
<script type="text/javascript">

    $(document).ready(function () { console.log('document/ready' + new Date()); });

    $(window).load(function () { console.log('window/load' + new Date()); });

</script>

<a href="tmp.aspx?a=1">tmp</a>
</form>

When I first visit page I get two outputs on console, one for document/ready and one for window/load. When I refresh page two more are quickly output, and instantly after that two more (from new page view). If I instead just click the link (tmp.aspx) which goes directly back to same page, this does not happen.

I am sure there is a good explanation for this.

EDIT:
The additional calls to $(document).ready() and $(window).load() are made BEFORE that page has refreshed. So when I first load the page they methods are called once, then I hit refresh and BEFORE the page has reloaded the methods are called again. After that, when the page just have been reloaded, the methods are called a THIRD time.

Answer

Behavior observed on 14.0.835.202. edit : (on Windows Seven x64)

It’s not the jquery fault : The DOMContentLoaded is fired another time just before page unload.

Simple test to check this :

 function startpage() {   
     console.log('page loaded');
   }   

   function unloadPage(){
       console.log("page unloaded");
   }
document.addEventListener("DOMContentLoaded", startpage, false);
window.onbeforeunload = unloadPage;

You should see after a refresh:

page loaded
page loaded // should not be here and is not on Firefox.
page unloaded
loaded

In your console (with persistence on)

I think it’s simply a Chrome bug. Not a console one, as timestamping proves it’s not a duplicate.

Edit : the same Chrome version but running OSX seems ok (see comment below). It tends to confirm that it’s a bug.



Source: stackoverflow