spacer

home / experts / dhtml / diner / beforeunload
DHTML Diner Logo

This is an Internet Explorer 4+ for Windows technique.The in-page examples will only work in Explorer 4+ Windows.

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The IE onBeforeUnload Event Handler

As 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!

Compatibility

This 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 Purists

If 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

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

All Rights Reserved. Legal Notices.
Created: Mar 23, 1999
Revised: Mar 23, 1999

URL: http://www.webreference.com/dhtml/diner/beforeunload/bunload3.html