Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 272
Sten Avatar asked Aug 26 '11 12:08

Sten


2 Answers

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.

like image 135
mddw Avatar answered Sep 18 '22 05:09

mddw


In Chrome 13 for Mac I am not seeing this behavior. After you load the page for the first time, and before you hit refresh, do you clear out the console? Is there a chance you are seeing the old console output (I know some browsers like to keep a previous page's console output around)?

I would not expect those two events to be fired twice on a refresh, so my guess would be stale console, either by design because it was a manual refresh or perhaps Chrome 14 has bugs with their dev console.

like image 40
Nick B Avatar answered Sep 21 '22 05:09

Nick B