Javascript Reference
Categories: onbeforeunload

javascript onbeforeunload Description

@July 1, 2009, 10:34 a.m.
onbeforeunload Firefox/Netscape/NN n/a IE 4(Win)/5(Mac) Chrome/Safari/DOM n/a  

 

Bubbles: No; Cancelable: Yes  

Fires just before the current document begins to unload due to impending navigation to a new page, form submission, or window closure. This event fires before the onunload event, and gives your scripts and users a chance to cancel the unload action. Some of this activity is automatic to prevent nefarious scripts from trapping users on a page.

 

In the onbeforeunload event handler, assign a string to the event.returnValue property to force IE to display a dialog box that lets the user choose whether the page should stay where it is or the navigation or window closure action that the user requested continues as expected. The string assigned to the event property becomes part of the dialog box message (other text in the message is hardwired by the browser and may not be removed or modified). The resulting action is controlled by the user's button choice in the dialog box.

 
Typical Targets

The body and frameset elements, plus the window object.


How to cancel onunload?
<body onbeforeunload="return false;">
<body onbeforeunload="return YourBooleanFunction();">

If return false, browser will confirm with user again which means you can't stop user from closing web page.


Powered by Linode.