Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
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
 
Old March 4th, 2009, 08:46 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
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/
 
Old March 4th, 2009, 10:04 PM
Friend of Wrox
 
Join Date: Jun 2008
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.





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





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