| home / experts / dhtml / diner / beforeunload |
This is an Internet Explorer 4+ for Windows technique.The in-page examples will only work in Explorer 4+ Windows. |
The IE onBeforeUnload Event HandlerAs mentioned, the beforeunload dialog is built-in to Explorer, like the alert, confirm and prompt dialogs. It provides a standard beginning and end for your message:
Your custom message should give a reason for the user not to leave the page: window.onbeforeunload = bunload;
function bunload(){
mess = "You will lose all information provided\nduring navigation of this site";
return mess;
}
Notice the use of an escaped "\n" to force a new line. The dialog displays text, not HTML, so we use the text-standard "\n". In the above code, we ask the dialog to display the "You will lose all information provided during navigation of this site" (whatever that means) message on two lines. The resulting display is:
The user has been warned and will never blame you again! CompatibilityThis event handler works in IE4+ for Windows only. All other JavaScript-enabled browsers will not choke on it, since they will consider onbeforeunload to be a user-defined custom method of the window object. Perfectly acceptable. They will parse the statements but they will never be executed. Built-in compatibility! For IE PuristsIf you are working in an IE4+ WIN-only environment, the onbeforeunload handler can also be defined using the FOR= and EVENT= attributes of the SCRIPT tag, presently supported only by Explorer: <SCRIPT LANGUAGE="JScript" TYPE="text/javascript" FOR=window EVENT=onbeforeunload> mess = "You will lose all information provided\nduring navigation of this site" return mess; </SCRIPT> Even though the statements are protected by the LANGUAGE="JScript" attribute, Navigator will still regard the return as a stray return outside a function and generate an error. Use the above syntax only in a 100% IE4+ environment. On the final page, we'll look at a simple real-world use. |
Produced by Peter Belesis and
All Rights Reserved. Legal Notices.
Created: Mar 23, 1999
Revised: Mar 23, 1999
URL: http://www.webreference.com/dhtml/diner/beforeunload/bunload3.html