How to detect browser closing and navigating to some other page

Let’s suppose on your website you have a form. When the user fills the form and navigates to a different URL location, you want to warn the user that you will lose your form data. Check out the following code.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<script type="text/jscript">
var leaving=true;
window.onbeforeunload = function(e)
      if (!e) e = event;
      if (leaving) e.returnValue = "You might loose your changes";

  <a href="">Leave</a>
<a href="page1.htm" onClick="leaving=false">stay on site</a>

Next Post Previous Post
No Comment
Add Comment
comment url