Friday, April 20, 2012

Detecting browser closing and navigating to some other page

In this post am going to explain about how detect the browser closing and if the user wish to move or leave the current webpage then need to redirect the new page.
To Do this create new Aspx page and copy the following code :

<html>
  <head>
<script type="text/jscript">
    var leaving = true;
    window.onbeforeunload = function (e) {
        if (!e) e = event;
        if (leaving) e.returnValue = "your leaving the page";
    }

</script>
  <title></title>
  </head>
  <body>
  <a href="http://www.google.com/">Leave Page</a>
<a href="page1.htm" onClick="leaving=false">stay on site</a>

  </body>
</html>


If you see the code Am wrote one javascript code with the event “    window.onbeforeunload” This is An event that fires before the unload event when the page is unloaded.
Syntax :
window.onbeforeunload = funcRef

1)funcRef is a reference to a function or a function expression
2) The function should assign a string value to the returnValue property of the Event object and return the same string 

Output :


No comments:

Post a Comment