HTML » HTML Core Attributes » onbeforeunload


This attibute is used to specifiy an event handler for the onBeforeUnload event, which fires before a page is unloaded.

This attribute is typically used to provide the user with information prior to unloading a page, as well as giving the user a chance to remain on the page.


function ask()
  event.returnValue = "Any information you have entered on this page will be lost. Are you sure?";


<BODY onbeforeunload="Ask()">

Assigning a string to event.returnValue will cause a dialog box to display and ask the user whether or not they wish to remain on the page.

The assigned string will be preceded by the default message, "Are you sure you want to navigate away from this page?" and followed by the message, "Press OK to continue, or Cancel to stay on the current page."

Language(s): JavaScript