Solved

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

Posted on 2001-07-20
9
216 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 14

Expert Comment

by:avner
Comment Utility
garthmiller,
If you are satisfied with the answer for this question , please close it and don't leave open question.
Thanks .
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:garthmiller
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Cool.

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now