• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 846
  • Last Modified:

How to detect browser closing and navigating to some other page

Hello Experts,

I need a java script to pop a message to the user in the following cases:
    1. User navigating to some other page
    2. User closes the broser. both close and X

Thanks,
0
sunny012097
Asked:
sunny012097
4 Solutions
 
clockwatcherCommented:
0
 
Michel PlungjanIT ExpertCommented:
this used to work...

<html>
<head>
<title>Close or refresh</title>
<script type="text/javascript">
<!--
var ie = navigator.userAgent.toLowerCase().indexOf('msie')!=-1;
function closeOrRefresh() {
  var top=self.screenTop;
  if (top>9000) alert('Close');
  else alert('Refresh');
}

//--></script>

</head>
<body onLoad="window.leaving=true"
onUnload="if (window.leaving) {
  if (ie) closeOrRefresh();
else { // ns unloads scripts from the page first, hence the script must be in the attribute
  var topW=window.outerWidth;
  if (topW==0) opener.alert('window was closed'); // needs an opener else a window.open
  else opener.alert('window was Refreshed');
}
}">

<a href="http://www.google.com/">Leave</a>
<a href="page1.html" onClick="window.leaving=false">stay on site</a>
</body>
</html>
0
 
StingRaYCommented:
Place this into your code

<script type="text/javascript">
window.onunload = function() { alert("Bye"); }
</script>

This will alert "Bye" when user close the browser or navigate to somewhere else.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Michel PlungjanIT ExpertCommented:
onUnload will trigger ALSO when clicking a link
0
 
sunny012097Author Commented:
how can I confirm the exit?
0
 
Michel PlungjanIT ExpertCommented:
Only in IE using

<body onLoad="window.leaving=true"
 onBeforeUnload="if (leaving) returnValue='You are leaving'">
<a href="http://www.google.com/">Leave</a>
<a href="page1.html" onClick="window.leaving=false">stay on site</a>
</body>
0
 
StingRaYCommented:
Try this

window.onbeforeunload = function(e)
{
      if (!e) e = event;
      e.returnValue = "Any string";   // You can place any string here. The string will be displayed on confirmation dialog
}

This is working on both IE and FF.

0
 
Michel PlungjanIT ExpertCommented:
Interesting
So FF implemented this too

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
<script>
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.html" onClick="leaving=false">stay on site</a>

  </body>
</html>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now