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">
<html>
  <head>
<script type="text/jscript">
var leaving=true;
window.onbeforeunload = function(e)
{
      if (!e) e = event;
      if (leaving) e.returnValue = "You might loose your changes";
}

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

  </body>
</html>

Please do not post any spam link in the comment box😊

إرسال تعليق (0)
أحدث أقدم