Tuesday 12 February 2013

Alert User on Browser Navigation Using Javascript


Alert User on Browser Navigation Using Javascript


We always come into the situation when we have to alert User before navigating to another page or during Back Button Press in order to ensure that the data present in the current page is not lost suddenly..

Here is how to achieve this.. Works on Firefox and Internet Explorer..


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">

    <title>Capture Browser Navigation</title>
   
    <script type="text/javascript">
    function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
    }
    window.onbeforeunload=goodbye;
    
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <a href="http://google.com">click here</a>
        <input type="submit" value="Button" />
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top