Wednesday, October 17, 2007

onbeforeunload Javascript

There are some cases where you might want to instruct the user before he/She is navigating to the next page. That might be a meaningful message like "Are you sure you want to navigate away from the current page?"
Probably end user pressed the X button by mistake and he/she might loose his/her current state

There is onbeforeunload event for the JavaScript that fires when you are navigating away from the current page.
Here is the Code for that.



script type="text/javascript"
function close()
{
event.returnValue = "This will navigate to next page.";
}
script




body onbeforeunload="close()"

a href="microsoft.com" Click Here to navigate to Microsoft a

body

html






Note:
The default statement that appears in the dialog box, "Are you sure you want to navigate away from this page? ... Press OK to continue, or Cancel to stay on the current page.", cannot be removed or altered.

No comments: