onbeforeunload Javascript

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.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.