Tutorials » Javascript tutorials »

Playing with window events


This article explains about onbeforeunloadevent


The approach


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.

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

<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.




Next Chapter: How to call JavaScript from code behind?
Previous Chapter: Understanding Events
More Chapters: Javascript Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    Manigandan
    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India