Link to home
Start Free TrialLog in
Avatar of garthmiller
garthmiller

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of avner
avner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of garthmiller
garthmiller

ASKER

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')
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>
garthmiller,
If you are satisfied with the answer for this question , please close it and don't leave open question.
Thanks .
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?
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..
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>
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>
Cool.

so the "window.event.clientX " works for Mac ?