AJAX - The Complete Reference

(avery) #1

456 Part II: Developing an Ajax Library^


They have also used techniques on the unload of the page:

window.unload = function () {window.history.forward(1);};

The downside with this is that the page has unloaded, so when it forwards the user, it
puts them back to the problem state of the application:

If you think that these techniques seem a bit hackish, you aren’t alone. This isn’t the right
approach in most cases, and it turns out that the premature unload problem is such a prevalent
issue that the major browser vendors have adopted a psueudo-standard onbeforeunload
handler to address the concern. We add this small bit of code to our application:

window.onbeforeunload = function () {return "";};

The user is now prompted with a dialog to warn that they will lose position in the Web
application if they continue.

If we desire, a message string can be passed back in the onbeforeunload handler to
customize the message:

window.onbeforeunload = function () {return "Are you sure you want to leave
the Photo viewer?";};
Free download pdf