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