Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 4th, 2009, 06:04 AM
Registered User
 
Join Date: Mar 2009
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to Detect when the user clicks on close button

Hi,
I want to call a function,when the user clicks on close button ,
for the above problem i tried onbeforeunload and onunload function,by using these function i am able to call a function while user closing a browser,but my problem is the same functions(onbeforeunload and onunload ) are calling when the user trying to change the urls also.
but my requirment is the function should call only when the user clicks on close button.
Thanks in advance
Nanajee
Reply With Quote
  #2 (permalink)  
Old March 4th, 2009, 08:46 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can't reliably differentiate. Knowing whether they closed the browser as opposed to visted a different site is considered an invasion of privacy and outside your remit so you'll need to approach the problem a diffeent way.
What exactly are you trying to achieve?
__________________
Joe
http://joe.fawcett.name/
Reply With Quote
  #3 (permalink)  
Old March 4th, 2009, 10:04 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

And what about when the user just types a new URL into the address bar? Or selects a URL from his/her Favorites? Or or or...

The best you can do--and it's not easy but it is possible--is to find out if the user is going to another page (or even the same page) on your site or is leaving your site.

And the way you have to do that is keep track of EVERY SINGLE WAY on the current page that they *CAN* go to another page (or reload same page) on YOUR site.

So what you can do is set a JS variable to false:
Code:
<script>
var stayingOnMySite = false;
</script>
And then EVERY PLACE where the user might (for example) submit a <FORM> or click on an <A HREF> that will keep them on your site, you must CHANGE the flag to true.

Then, finally, in the onunload or onbeforeunload code, you can check that flag and act accordingly.

Now... There are *STILL* some things you can't possibly catch. For example, if the user hits F5 (refresh) or hits the BACK button, you can't "see" those and so you will think the user *IS* leaving your site, even though he/she may not be.

But this is about as close as you can come.

You have to think long and hard about whether this very imperfect level of detectability is worth the effort.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count the button clicks bex ASP.NET 2.0 Basics 2 February 13th, 2009 05:34 AM
Refresh Page when User Clicks on Back Button testsubject Visual Studio 2005 1 June 26th, 2006 03:46 AM
Detect double line command button caption ysiline Access VBA 5 April 26th, 2006 09:30 PM
how to detect in asp.net browser window close connect2sandep ASP.NET 1.0 and 1.1 Professional 1 October 31st, 2005 05:01 AM
How to detect window Close (X) Button cyberjames2003 Javascript How-To 2 July 27th, 2003 08:13 PM



All times are GMT -4. The time now is 08:20 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.