How can I tell when a user closes the window on a Mac?

On a Mac when a user closes a browser window, the browser
application keeps on running.  Sometimes a user might forget that the application is still going because all the windows are closed.  However, if another user comes along and opens a new window, that new window will retain the history of the first user.

I want to warn users to exit the application when the close the browser window from one of my pages.

A javascript alert message would be just right.

Here is what I have tried:

window.onUnload = function winUnload () {
if (window.closed == true)
  {
    alert("To log off you must exit the application");
  }
}

However, since the onUnload event takes place before the window is closed, what I have doesn't work right.

I want the alert message to be displayed only when the user closes the browser window.  Not if they just move to another page.

This isn't a problem on PC's because when the user closes the browser window, the application closes too.
garthmillerAsked:
Who is Participating?
 
avnerConnect With a Mentor Commented:
try this code , it'll show you if the user is moving to a different page or closing thepage , it works in NS and IE on PC , I've never tested it on Mac :

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>that</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function RorC() {

var top=self.screenTop;
if (top>9000)
{alert('window was closed')}
else
{alert('window was Refreshed')}
}

//-->
</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFFF" onunload="javascript:if (document.all){RorC()} else {var top=window.outerWidth;if (top==0) {opener.alert('window was closed')} else {opener.alert('window was Refreshed')}}">
gfds
</BODY>
</HTML>


0
 
garthmillerAuthor Commented:
What you suggest almost works.  Here is my code:

<script language="javascript">
<!--
function winUnload () {
     if (document.all) {
          var top=self.screenTop;
          if (top>9000) alert('window was closed');
          else alert('window was Refreshed');
     }
     else {
          var top=window.outerWidth;
          if (top==0) alert('window was closed');
          else alert('window was Refreshed');
     }
}
// -->
</script>

<body onunload="winUnload();">

This works on Netscape for a Mac but not on IE for a Mac.

In your code what is: opener.alert('window was closed')

Is it supposed to be just: alert('window was closed')
0
 
farajCommented:
you could also delete the history :

The location object's replace() method overwrites the current history entry with the specified URL. It removes the current history entry before the next URL is loaded. Removing the item from the history list also prevents users from seeing the page again by clicking on the Back button later.
This technique is very useful for online games and multi-page forms, where you want prevent the user from returning to the previous page. Furthermore, the location.replace() method is supported by all browsers except Navigator 2.0x. The following example shows how to use this method in a link:


<A HREF="javascript:location.replace('nextpage.html')">Advance</A>
0
Get expert help—faster!

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

 
avnerCommented:
garthmiller,
If you are satisfied with the answer for this question , please close it and don't leave open question.
Thanks .
0
 
garthmillerAuthor Commented:
avner,

what you have given me solves half the problem.  It works for Netscape on a Mac but not for IE on a Mac.

For IE on a Mac self.screenTop always has the value "undefined".

I need a solution that works for both IE and Netscape.  So I'm not quite satisfied yet.

Any ideas what will work for IE on a Mac?
0
 
avnerCommented:
since I don't have a mac to test things I cannot tell you . but It should be a problem to find the equavalent of screenTop in Mac , I guess..
0
 
garthmillerAuthor Commented:
Here is what I finally got to work for both IE and Netscape:

<script language="javascript">
<!--
window.onunload = function winUnload () {
     if (navigator.userAgent.indexOf("Mac") != -1) {
          warn = false;
          if (document.all) {
               if (window.event.clientX < 0) warn = true;
          }
          else {
               if (window.outerWidth == 0) warn = true;
          }
          if (warn) alert("You must close the application to log off");
     }
}
// -->
</script>
0
 
garthmillerAuthor Commented:
Here is what I got to work on IE and Netscape:

<script language="javascript">
<!--
window.onunload = function winUnload () {
     if (navigator.userAgent.indexOf("Mac") != -1) {
          warn = false;
          if (document.all) {
               if (window.event.clientX < 0) warn = true;
          }
          else {
               if (window.outerWidth == 0) warn = true;
          }
          if (warn) alert("You must close the application to log off");
     }
}
// -->
</script>
0
 
avnerCommented:
Cool.

so the "window.event.clientX " works for Mac ?
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.

All Courses

From novice to tech pro — start learning today.