Solved

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

Posted on 2001-07-20
9
218 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:garthmiller
  • 4
  • 4
9 Comments
 
LVL 14

Accepted Solution

by:
avner earned 300 total points
ID: 6303615
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
 

Author Comment

by:garthmiller
ID: 6303683
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
 
LVL 1

Expert Comment

by:faraj
ID: 6305998
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 14

Expert Comment

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

Author Comment

by:garthmiller
ID: 6308639
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
 
LVL 14

Expert Comment

by:avner
ID: 6308655
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
 

Author Comment

by:garthmiller
ID: 6309096
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
 

Author Comment

by:garthmiller
ID: 6309103
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
 
LVL 14

Expert Comment

by:avner
ID: 6309104
Cool.

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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

803 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