Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2001-07-20
9
Medium Priority
?
231 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 900 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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

885 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