Monday, March 10, 2008

JavaScript - Display confirmation before the page is closed

If you need to display a confirmation before the user closes the window, you can use the beforeunload event of the window object to display a confirmation message.
JavaScript:
function AddUnloadHandler()

{
AddEventHandler(window, 'beforeunload', HandleUnload);
}
function HandleUnload()

{
var strConfirm = 'Please make sure you saved your changes before
closing the page.';
if (document.all)

{
window.event.returnValue = strConfirm;
}
else

{
alert(strConfirm);
var evnt = arguments[0];
evnt.stopPropagation();
evnt.preventDefault();
}
}

ASPX:
<body onload="AddUnloadHandler()">

No comments: