By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,167 Members | 1,629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,167 IT Pros & Developers. It's quick & easy.

Capturing event user leaving page

P: n/a
Hi!

Is it possible to capture an event when user leaves the page and
execute some code? Not when he closes the window.

Any ideas?
Sep 2 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Zvonko" <zv****@velepromet.hr> wrote in message
news:df**********@ss405.t-com.hr...
Hi!

Is it possible to capture an event when user leaves the page and execute
some code? Not when he closes the window.

Any ideas?


<head>
<script type="text/Javascript">
function yourFunction() {
alert("About to leave...");
}
</script>

<body onunload="yourFunction();">
...
</body>

--
Dag.
Sep 2 '05 #2

P: n/a
Dag Sunde said the following on 9/2/2005 9:53 AM:
"Zvonko" <zv****@velepromet.hr> wrote in message
news:df**********@ss405.t-com.hr...
Hi!

Is it possible to capture an event when user leaves the page and execute
some code? Not when he closes the window.

Any ideas?

<head>
<script type="text/Javascript">
function yourFunction() {
alert("About to leave...");
}
</script>

<body onunload="yourFunction();">
...
</body>


Are you positive that the alert will get fired when the page is left?

Power goes off, no alert......

Besides, I left. Why do you want to annoy me further with alerts, I
left, leave me be in peace.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 2 '05 #3

P: n/a
"Randy Webb" <Hi************@aol.com> wrote in message
news:gI********************@comcast.com...
Dag Sunde said the following on 9/2/2005 9:53 AM:
"Zvonko" <zv****@velepromet.hr> wrote in message
news:df**********@ss405.t-com.hr...
Hi!

Is it possible to capture an event when user leaves the page and execute
some code? Not when he closes the window.

Any ideas?

<head>
<script type="text/Javascript">
function yourFunction() {
alert("About to leave...");
}
</script>

<body onunload="yourFunction();">
...
</body>


Are you positive that the alert will get fired when the page is left?

Power goes off, no alert......

LOL

Of course I'm not sure. But the OP didn't specify nuclear strikes or
power outages. (And I *do* have an UPS). :D
Besides, I left. Why do you want to annoy me further with alerts, I left,
leave me be in peace.


Couldn't agree more.

But Now I have informed him how to do it (Not taking power failures
into account), and you have told him why he shouldnt. All we can do
now is to hope he use the knowledge and advice wisely.

--
Dag.
Sep 2 '05 #4

P: n/a
I would add that onUnload is fired when, really, the user has already
left (The request for a new page has already been sent to the server).
IE has onBeforeUnload which will actually catch the user before they
have issued the request although I doubt this is available for Firefox
or much else.

--------- beegee

Sep 3 '05 #5

P: n/a
bg*****@gmail.com wrote :
I would add that onUnload is fired when, really, the user has already
left (The request for a new page has already been sent to the server).
IE has onBeforeUnload
and Mozilla 1.7, Seamonkey 1.x and Firefox 1.x all have onbeforeunload
too since March 2004.

which will actually catch the user before they have issued the request although I doubt this is available for Firefox
or much else.


Gérard
--
remove blah to email me
Sep 4 '05 #6

P: n/a
Randy Webb wrote:

Are you positive that the alert will get fired when the page is left?

Power goes off, no alert......

Besides, I left. Why do you want to annoy me further with alerts, I
left, leave me be in peace.


Hi Randy.

I don't want to bother bother my visitors. The idea is that when the
user leaves the page to automatically log him off. I want that if the
user hit the Back button on the browser his session is timeouted and he
must login to access the page. Is this possible?
And if you have a web with multiple pages, how to implement the idea
with onunload? It would trigger the logoff even if the user is going to
another page inside the web site, and I don't want that. I would like to
log him off only when he is leaving the site.

Thanks for the answer.

Bye

Sep 5 '05 #7

P: n/a
Zvonko said the following on 9/5/2005 2:14 AM:
Randy Webb wrote:

Are you positive that the alert will get fired when the page is left?

Power goes off, no alert......

Besides, I left. Why do you want to annoy me further with alerts, I
left, leave me be in peace.

Hi Randy.

I don't want to bother bother my visitors. The idea is that when the
user leaves the page to automatically log him off. I want that if the
user hit the Back button on the browser his session is timeouted and he
must login to access the page. Is this possible?
And if you have a web with multiple pages, how to implement the idea
with onunload? It would trigger the logoff even if the user is going to
another page inside the web site, and I don't want that. I would like to
log him off only when he is leaving the site.

Thanks for the answer.


You use server-side sessions and timeouts.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Sep 5 '05 #8

P: n/a
Zvonko wrote:
I don't want to bother bother my visitors. The idea is that when the
user leaves the page to automatically log him off. I want that if the
user hit the Back button on the browser his session is timeouted and he
must login to access the page. Is this possible?
And if you have a web with multiple pages, how to implement the idea
with onunload? It would trigger the logoff even if the user is going to
another page inside the web site, and I don't want that. I would like to
log him off only when he is leaving the site.

Thanks for the answer.


We have this problem too.

Our web server talks to a back end server that is written in a legacy
3GL with a contrived remote call facility which is licensed per session.
It's a monothread language, and the whole setup uses convoluted gobbets
of global data shared between *hundreds* of functions (it's *years*
old), so one web session consumes one back end server session for its
duration.

It's imperative that whenever a user closes their browser or navigates
away that the back end server session is closed down.

They have implemented the front page of the application as a frameset
(shudder!) with one frame in it - the application's login page. The
frameset has an onunload() method which pops up a window using
window.open("logout.jsp"). The logout.jsp closes the back end session
and sends a "window.close()" script to close the popped-up window. All
navigations within the app take place *within* the frame, so the
onunload is not triggered until the browser is closed or a new URL is
entered into the navigation bar.

I know, I know - it's ugly and unreliable (sudden power outages), but
it's what was there when I arrived, I've just tidied it up and made it a
little more reliable than it was.

I have thought of changing the method and implementing some kind of
"heartbeat" using window.setInterval() in every page's "onload" method
(we have a custom tag library which is used to create the page layout
which could do this), and XMLHttpRequest which sends an "I'm still here"
request every 10 seconds or so. The session would then have a
java.util.TimerTask scheduled to close the session down which would be
cancelled and re-scheduled upon receipt of that request.

Either way, it's not *really* supported, and it's going to be a hack.

Good luck, and post back your implemented solution.

Nige

Sep 10 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.