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.

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

<body onbeforeunload="close()">

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




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

    Awards & Gifts

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India