views:

474

answers:

1

Hi,

I haven't seen any mention of this is David Flanagan's "JavaScript - the Definitive Guide" book (I've asked this question on O'Reilly Answers too), or anywhere else for that matter, but I am finding that window.onunload is not invoked unless the document fully loaded, which is quite annoying! That's true for IE8 and Chrome 3 at least

I have a piece of Javascript I want to add to the bottom of web pages. If the user happens to click off the page before the page has loaded(^ see below), and before my Javascript has had a chance to run, I was hoping to trap that in the onunload. But that's not possible as the onunload event handler is only invoked when the document fully loads. If I can't use onunload, is there anything else I can use to achieve the same?

^ "Javascript - The Definitive Guide" suggests user interaction with the page cannot happen until the page is loaded. I believe this is wrong. Here is what the book says:

"Another grey area in the Javascript execution model is the question of whether event handlers can be invoked before the document is fully loaded. Our discussion of the Javascript execution model has so far concluded that all event handlers are always triggered after all scripts have been executed. While this typically happens, it is not required by any standard. If a document is very long or is being loaded over a slow network connection, the browser might partially render the document and allow the user to begin interacting with it (and triggering event handlers) before all scripts and onload handlers have run. If such an event handler invokes a function that is not yet defined, it will fail. (this is one reason to define all functions in scripts in the of a document.) And if such an event handler attempts to manipulate a part of the document that has not yet been parsed, it will fail. This scenario is uncommon in practice, and it is not usually worth the extra coding effort required to aggressively protect against it." (5th Edition, Page 256)

I performed tests on IE8 that showed it was possible to interact with the page (and leave it via an anchor link) at any stage in the loading of the page.

Thanks for any help, Paul

A: 

It makes sense to me that an onunload event could not fire until the document is parsed. Document parsing and script execution generally run on the same thread and if a user was to navigate away from the page then the thread would be cancelled after the onunload event is fired. Since the parsing is using the thread at the time the user navigates away from the page, the browser would have the following options:

  • Complete the parsing and fire the unload event - which would delay navigation to the next page, so this isn't really a feasible option.
  • Cancel parsing and fire the unload event - this is a more feasible option but you could probably see some UAs not bothering.
  • Just stop the thread and navigate to the next page - which seems to be what's happening for you in IE and Chrome

Maybe you could see if the onbeforeunload event is fired? I don't think it will make a difference though. I think the best thing to do would be to look at ways you can optimize the page to load faster.

Andy E