473,395 Members | 1,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting event when user clicks X in browser, something different from {before}bodyunload

Hi all,
I am trying to get a way by which I'll know exactly when user goes out
of my site by clicking on close button in browser, So that w/e user
click close button in browser, I can send a signal to server.
This seems to be achievable with body unload events, but it is little
too much, as even if user navigate within my site, this event will be
generated, this can be avoided by handling onclick of each link, so
that I'll know exactly which link is clicked, but honestly this looks
over doing to me, any other smart short cut for finding out when user
click on X in browser?

TIA
Hemant

Sep 3 '06 #1
6 3901
he**********@gmail.com wrote:
I am trying to get a way by which I'll know exactly when user goes out
of my site by clicking on close button in browser, So that w/e user
click close button in browser, I can send a signal to server.
You can't. Nor can you tell when the user "closes" their browser by turning
their computer off, having their Internet connection fail, etc, etc.
--
David Dorward <http://blog.dorward.me.uk/ <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 3 '06 #2
he**********@gmail.com wrote:
Hi all,
I am trying to get a way by which I'll know exactly when user goes out
of my site by clicking on close button in browser, So that w/e user
click close button in browser, I can send a signal to server.
This seems to be achievable with body unload events, but it is little
too much, as even if user navigate within my site, this event will be
generated, this can be avoided by handling onclick of each link, so
that I'll know exactly which link is clicked, but honestly this looks
over doing to me, any other smart short cut for finding out when user
click on X in browser?

TIA
Hemant
Except the body.onunload event/function, there's no other solution for
this. May I suggest that you create a general function as this :

(Untested !)

window.gotoLink( url, disconnect ) {
// if disconnect is undefined or false
if ( !disconnect ) {
window.noUnloadWarning = true; // do not warn when unloading the
page
}

document.location.href = url; // use Javascript to load this url
into the current browser
};
// ....
window.body.onunload = function(e) {
// if window.noUnloadWarning is not set, it means that we're not
// refreshing the page with a new link, but the user tries to close
// the browser
if ( !window.noUnloadWarning ) {
// if confirm is 'No', we simply return false to the event, and the
browser stays
// active
if ( !confirm( "You are about to close your browser. " +
"Do you really want to terminate your " +
"session on this site ?" ) ) {
return false; // returning false will not unload, thus not
close the browser
}
}
return true;
};
------------- eoc -------------------

and call this function in all your anchors (<aelements) such as :

<a href="#"
onclick="gotoLink('http://www.blah.com&var=foo&var2=bar');">Click me
!</a>
Good luck !

-Yanick

Sep 3 '06 #3
Yanick said the following on 9/3/2006 9:15 AM:
he**********@gmail.com wrote:
>Hi all,
I am trying to get a way by which I'll know exactly when user goes out
of my site by clicking on close button in browser, So that w/e user
click close button in browser, I can send a signal to server.
This seems to be achievable with body unload events, but it is little
too much, as even if user navigate within my site, this event will be
generated, this can be avoided by handling onclick of each link, so
that I'll know exactly which link is clicked, but honestly this looks
over doing to me, any other smart short cut for finding out when user
click on X in browser?

TIA
Hemant

Except the body.onunload event/function, there's no other solution for
this.
The onunload event is not even close to a solution though. The
"solution" to the problem is to solve it on the server. Use Sessions and
Timeouts and your problem is solved.
May I suggest that you create a general function as this :
You can suggest it, but it still won't do what is requested.

Test your code, get the bugs out. Then load your page and then click the
Refresh button to reload the page....
(Untested !)

window.gotoLink( url, disconnect ) {
One of the problems with untested code is you don't find flaws such as
your "disconnect" flaw. It is never defined anywhere in your code so it
will always be undefined. If it is always going to have one state, there
isn't a whole lot of point in testing it is there?
// if disconnect is undefined or false
if ( !disconnect ) {
window.noUnloadWarning = true; // do not warn when unloading the
page
}

document.location.href = url; // use Javascript to load this url
into the current browser
};
// ....
window.body.onunload = function(e) {
// if window.noUnloadWarning is not set, it means that we're not
// refreshing the page with a new link, but the user tries to close
// the browser
That comment is utterly false and *very* misleading. There are other
ways to trigger the onunload without closing the browser.

Try clicking the Back button.....
<snip>
and call this function in all your anchors (<aelements) such as :

<a href="#"
onclick="gotoLink('http://www.blah.com&var=foo&var2=bar');">Click me
!</a>
That is even worse. It falls into the degradation problem where non-JS
browsers (or an error in the page) makes the page useless.

If you insist:

<a href="http://www.blah.com" onclick="return gotoLink(this.href)">My
Link</a>

Why do people insist on trying to make scripting harder than it is?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 3 '06 #4
Randy :

How nice of you to always point out other person's errors. It really
makes me feel like I tried, doesn't it ? I shall say that you post is
no better since it doesn't give any further solution to the problem.
The onunload event is not even close to a solution though. The
"solution" to the problem is to solve it on the server. Use Sessions and
Timeouts and your problem is solved.
How would that help to know when the user closes the browser versus or
simply clicked on a link ?
That comment is utterly false and *very* misleading. There are other
ways to trigger the onunload without closing the browser.

Try clicking the Back button.....
Well, how do you know ! I didn't consider that. Except the critisizing
comment in the first statement, that is constructive.
Why do people insist on trying to make scripting harder than it is?
Maybe because some people insist on not giving them any examples with
clear solutions, but only conceptual suggestions... which, I may add,
are not always connected to the problem.

Like I said to you in an earlier post, I have no grief against you. I
believe that you are knowledgeable, and somewhat ressourceful. Though
everytime I read your posts, I can't help to feel that you're putting
down those you tried to help before you.

As for the solution proposed, for the two reasons that you have
mentionned :
1) javascript issues
2) back button that'd fire the unload event
It is clear that the solution had issues. But I still stick to my
suggestion :

To he**********@gmail.com (the OP) :

This is a (tested) solution. It doesn't fix your back/foreward button
(you don't know if the 'X' or close button has been clicked, or if the
user simply press 'Back' or 'Foreward'), but it's a start (taken from
http://www.webreference.com/dhtml/di.../bunload4.html,
http://pro-thoughts.blogspot.com/200...oad-event.html) :

// take all anchors and disable the confirmation box
// in their onclick events
var anchors = document.getElementsByTagName('a');
var noUnloadConfirm = function() { setCloseConfirmation(false); };
for (var i=0; i<anchors.length; i++) {
anchors[i].onclick = noUnloadConfirm;
}
var closeConfirmationHandler = function(event){
//event = event||window.event;
var mess = "Wait! You haven't finished."
return mess;
}

function setCloseConfirmation(on){
window.onbeforeunload = (on) ? closeConfirmationHandler : null;
}

window.onunload = function() {
// if there's an event attached,
// it means that we confirmed,
// and the user agreed
if (window.onbeforeunload) {
alert( 'Unloaded !' );
}
};

setCloseConfirmation(true);

Good luck !

Yanick

Randy Webb wrote:
Yanick said the following on 9/3/2006 9:15 AM:
he**********@gmail.com wrote:
Hi all,
I am trying to get a way by which I'll know exactly when user goes out
of my site by clicking on close button in browser, So that w/e user
click close button in browser, I can send a signal to server.
This seems to be achievable with body unload events, but it is little
too much, as even if user navigate within my site, this event will be
generated, this can be avoided by handling onclick of each link, so
that I'll know exactly which link is clicked, but honestly this looks
over doing to me, any other smart short cut for finding out when user
click on X in browser?

TIA
Hemant
Except the body.onunload event/function, there's no other solution for
this.

The onunload event is not even close to a solution though. The
"solution" to the problem is to solve it on the server. Use Sessions and
Timeouts and your problem is solved.
May I suggest that you create a general function as this :

You can suggest it, but it still won't do what is requested.

Test your code, get the bugs out. Then load your page and then click the
Refresh button to reload the page....
(Untested !)

window.gotoLink( url, disconnect ) {

One of the problems with untested code is you don't find flaws such as
your "disconnect" flaw. It is never defined anywhere in your code so it
will always be undefined. If it is always going to have one state, there
isn't a whole lot of point in testing it is there?
// if disconnect is undefined or false
if ( !disconnect ) {
window.noUnloadWarning = true; // do not warn when unloading the
page
}

document.location.href = url; // use Javascript to load this url
into the current browser
};
// ....
window.body.onunload = function(e) {
// if window.noUnloadWarning is not set, it means that we're not
// refreshing the page with a new link, but the user tries to close
// the browser

That comment is utterly false and *very* misleading. There are other
ways to trigger the onunload without closing the browser.

Try clicking the Back button.....
<snip>
and call this function in all your anchors (<aelements) such as :

<a href="#"
onclick="gotoLink('http://www.blah.com&var=foo&var2=bar');">Click me
!</a>

That is even worse. It falls into the degradation problem where non-JS
browsers (or an error in the page) makes the page useless.

If you insist:

<a href="http://www.blah.com" onclick="return gotoLink(this.href)">My
Link</a>

Why do people insist on trying to make scripting harder than it is?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 4 '06 #5
Yanick said the following on 9/4/2006 11:40 AM:
Randy :

How nice of you to always point out other person's errors. It really
makes me feel like I tried, doesn't it ? I shall say that you post is
no better since it doesn't give any further solution to the problem.
It doesn't give any further solution because the solution doesn't lie in
client side scripting. The problem isn't detecting onunload - that is
trivial as you know. The problem is detecting *why* it was triggered and
you simply can't detect that.

Events that can trigger onunload:

Closing the Browser.
Closing the Tab (Tabbed Browsers).
Clicking a Link.
Submitting a Form.
Clicking a Button on the page that navigates away.
Clicking a Favorites.
Typing in a new URL.
Clicking the Forward/Back button.

That is not a complete list but just a sample.

Of those, you can deal with the third, fourth and fifth reasonably in
the page with scripting. The others you simply can not detect other than
the fact that the page is being unloaded.

It is an often enough asked question that it is in the group FAQ
although the section title it is in isn't the easiest to search for.

<URL: http://jibbering.com/faq/#FAQ4_29>

Which leads to this URL, which explains how to do it on the server:
<URL:
http://groups.google.com/group/comp.lang.javascript/msg/a0c582dc42c65fd2>
>Why do people insist on trying to make scripting harder than it is?

Maybe because some people insist on not giving them any examples with
clear solutions, but only conceptual suggestions... which, I may add,
are not always connected to the problem.
When you can't give a reliable solution then you there's not much point
in trying to come up with one. After you read questions like this a
kazillion times and explain it that many times you get jaded.
Like I said to you in an earlier post, I have no grief against you. I
believe that you are knowledgeable, and somewhat ressourceful. Though
everytime I read your posts, I can't help to feel that you're putting
down those you tried to help before you.
Maybe I need to change my demeanor and take a break from Usenet <shrug>
As for the solution proposed, for the two reasons that you have
mentionned :
1) javascript issues
2) back button that'd fire the unload event
It is clear that the solution had issues. But I still stick to my
suggestion :

To he**********@gmail.com (the OP) :

This is a (tested) solution. It doesn't fix your back/foreward button
(you don't know if the 'X' or close button has been clicked, or if the
user simply press 'Back' or 'Foreward'), but it's a start (taken from
http://www.webreference.com/dhtml/di.../bunload4.html,
http://pro-thoughts.blogspot.com/200...oad-event.html) :

// take all anchors and disable the confirmation box
// in their onclick events
Sidenote: You may want to look into adding the same type of
onclick/onsubmit event handlers to forms and buttons:

<button onclick="document.location....">
<input type="submit">

Also, I have seen code that looks like this:

<span onclick=""and <div onclick=""and <li onclick=""and it goes
on. Your code attempts to handle links but it can be made more
comprehensive but then it becomes overburdening to try to cover all the
possible ways to navigate away from the page.

The simplest solution to non A element onclicks is instead of setting
location properties, call a function that will cover the leaving of the
site.

Instead of this:

<button
onclick="document.location.href='http://www.google.com'">Google</button>

The page author would have to write it as such:

<button onclick="navigateAway('http://www.google.com')">Google</button>

And then have the navigateAway function set the unload flag.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 5 '06 #6
Hello all,
Big Thanks for wonderful examples :)

Actually I already have timeout on server which is of around 20mins,
But since it is high traffic site, It was worth giving a shot about
available options, But with the amount of risk and efforts involved I
realy dont think it makes any sense doing sessiontime out on browser
close.

Randy Webb wrote:
Yanick said the following on 9/4/2006 11:40 AM:
Randy :

How nice of you to always point out other person's errors. It really
makes me feel like I tried, doesn't it ? I shall say that you post is
no better since it doesn't give any further solution to the problem.

It doesn't give any further solution because the solution doesn't lie in
client side scripting. The problem isn't detecting onunload - that is
trivial as you know. The problem is detecting *why* it was triggered and
you simply can't detect that.

Events that can trigger onunload:

Closing the Browser.
Closing the Tab (Tabbed Browsers).
Clicking a Link.
Submitting a Form.
Clicking a Button on the page that navigates away.
Clicking a Favorites.
Typing in a new URL.
Clicking the Forward/Back button.

That is not a complete list but just a sample.

Of those, you can deal with the third, fourth and fifth reasonably in
the page with scripting. The others you simply can not detect other than
the fact that the page is being unloaded.

It is an often enough asked question that it is in the group FAQ
although the section title it is in isn't the easiest to search for.

<URL: http://jibbering.com/faq/#FAQ4_29>

Which leads to this URL, which explains how to do it on the server:
<URL:
http://groups.google.com/group/comp.lang.javascript/msg/a0c582dc42c65fd2>
Why do people insist on trying to make scripting harder than it is?
Maybe because some people insist on not giving them any examples with
clear solutions, but only conceptual suggestions... which, I may add,
are not always connected to the problem.

When you can't give a reliable solution then you there's not much point
in trying to come up with one. After you read questions like this a
kazillion times and explain it that many times you get jaded.
Like I said to you in an earlier post, I have no grief against you. I
believe that you are knowledgeable, and somewhat ressourceful. Though
everytime I read your posts, I can't help to feel that you're putting
down those you tried to help before you.

Maybe I need to change my demeanor and take a break from Usenet <shrug>
As for the solution proposed, for the two reasons that you have
mentionned :
1) javascript issues
2) back button that'd fire the unload event
It is clear that the solution had issues. But I still stick to my
suggestion :

To he**********@gmail.com (the OP) :

This is a (tested) solution. It doesn't fix your back/foreward button
(you don't know if the 'X' or close button has been clicked, or if the
user simply press 'Back' or 'Foreward'), but it's a start (taken from
http://www.webreference.com/dhtml/di.../bunload4.html,
http://pro-thoughts.blogspot.com/200...oad-event.html) :

// take all anchors and disable the confirmation box
// in their onclick events

Sidenote: You may want to look into adding the same type of
onclick/onsubmit event handlers to forms and buttons:

<button onclick="document.location....">
<input type="submit">

Also, I have seen code that looks like this:

<span onclick=""and <div onclick=""and <li onclick=""and it goes
on. Your code attempts to handle links but it can be made more
comprehensive but then it becomes overburdening to try to cover all the
possible ways to navigate away from the page.

The simplest solution to non A element onclicks is instead of setting
location properties, call a function that will cover the leaving of the
site.

Instead of this:

<button
onclick="document.location.href='http://www.google.com'">Google</button>

The page author would have to write it as such:

<button onclick="navigateAway('http://www.google.com')">Google</button>

And then have the navigateAway function set the unload flag.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Asit | last post by:
How can i know when user clicks X button of the browser ? I have to write some code when user clicks X button of browser. What is the event for capturing this event ? Help needed ASAP. Asit
2
by: Robert V. Hanson | last post by:
If you have per user information that you want to store in session and the setting in web.config is set to cookieless=false so you are intending on using cookies, the user's browser is set to not...
4
by: feng | last post by:
Hi, In our asp.net app, we need to capture the event when user close the browser window by clicking on the "x" button. But it doesn't seem to be as easy as it sound. Can someone show me how to...
3
by: Charlie | last post by:
Hi: I would like to clear session when user closes browser window by clicking the "x" in upper right hand corner. Since this is a client side event, how do I run server code (Session.Abandon())...
1
by: BerkshireGuy | last post by:
I have a several paged tab control on my form. On each page, there are several different controls. One page may have 5 controls and the other may have 10. Can I run specific code when one of...
9
RBPierce
by: RBPierce | last post by:
Hey all, hoping someone can help me with a workaround. I've got an event set to fire when a textbox changes or loses focus- no problems. However, when the user clicks into the box and Right Click-->...
1
by: swetha123 | last post by:
hello, Can any one please tell me I am Using php,Mysql,CSS to build my site In my database i have small images and large images i am show the small images to the right of the main container...
3
by: swetha123 | last post by:
hello, Can any one please tell me I am Using php,Mysql,CSS to build my site In my database i have small images and large images i am show the small images to the right of the main container...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.