[dojo-contributors] dojo.addOnUnload and window.onbeforeunload

James Burke jburke at dojotoolkit.org
Thu May 29 20:20:45 EDT 2008

A recent change I made:

uncovered an issue with having dojo.addOnUnload attached to
window.onbeforeunload. This issue has been there for a while, but the
above change made it more visible. See my bug comment here if you want
the gory details:

Executive Summary:
Clicking on a link or changing window.location to an URL that does not
destroy the current page (like an URL to download a file) will end up
destroying widgets/triggering callbacks registered via
dojo.addOnUnload. This probably makes the page unusable.

*All* browsers trigger the window.onbeforeunload event when
window.location change happens as described above, but window.onunload
*is not* triggered.

Possible outcomes:
1) Document this and live with it.

2) Consider changing dojo.addOnUnload to attach to window.onunload.
Then also introduce a dojo.addOnBeforeUnload() for code that wants to
attach to that (probably dojox.cometd and dojox.analytics -- all other
dojo.addOnUnload usage in the toolkit is for IE leak prevention).

#2 does not seem viable since I believe the DOM is unstable by the
time window.onunload fires. We normally do not need to scan the DOM,
but more like call methods on DOM elements that we are already
referencing, but still, this seems risky. Can anyone confirm this
though? I am going off my hazy memory.

I also do not like changing the API in this fashion, since we will
likely break a few people, depending on how they use dojo.addOnUnload.

So, we'll probably go with #1, unless anyone has other ideas.


More information about the dojo-contributors mailing list