[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to detect browser closing and navigating to some other page

Posted on 2007-08-05
8
Medium Priority
?
842 Views
Last Modified: 2013-11-05
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
Comment
Question by:sunny012097
8 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 19635778
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 1200 total points
ID: 19636817
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
 
LVL 15

Expert Comment

by:StingRaY
ID: 19636955
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 19637146
onUnload will trigger ALSO when clicking a link
0
 

Author Comment

by:sunny012097
ID: 19648848
how can I confirm the exit?
0
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 1200 total points
ID: 19649174
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
 
LVL 15

Assisted Solution

by:StingRaY
StingRaY earned 300 total points
ID: 19652932
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
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 1200 total points
ID: 19653139
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question