Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to detect when the user closes the browser

Posted on 2001-06-14
7
234 Views
Last Modified: 2008-02-01
Hi,

Does anyone know of a way to detect when the user closes their browser (And only when they close their browser).

The only way I've discovered to do it, is to tie some code to the 'onUnload' event of the HTML body, but of course that gets triggered whenever the page gets unloaded, i.e. on refresh, back, etc, and not just when the user closes the browser.

Any ideas?

Is there some way I could use ASP to my advantage?

Many thanks,

Rob
0
Comment
Question by:robbiemorgan
7 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6193140
This is likely to be the best code you are going to get:

http://developer.irt.org/script/1578.htm

Ant
0
 
LVL 5

Accepted Solution

by:
nilapenn earned 100 total points
ID: 6193849
Try This


<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>
0
 

Author Comment

by:robbiemorgan
ID: 6194398
Perfect!! And I was beginning to think it just wasn't possible! Many thanks, Rob.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:pagemastah
ID: 6194808
does this run both in IE and Netscape?

this is effective!
0
 

Author Comment

by:robbiemorgan
ID: 6194892
Fortunately that's not a concern for me in this particular instance. But if you do get a chance to try it, please let me know!
0
 
LVL 3

Expert Comment

by:dnoelpp
ID: 6195065
nilapenn: How does this trick work? Just curious...
0
 
LVL 2

Expert Comment

by:sjhyam
ID: 6198052
Me Too...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS does not refresh 6 30
on window close, not beforeunload 6 18
Copying table data to one another 15 31
message Alert on an empty search 10 22
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

860 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