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

Detect window close and navigating away from a page

P: n/a
Hello,

I've been trying to detect when a user closes a page or navigates away
from it. This is part of a solution for locking pages (that is, if a
user enters a page, no one else can enter it while he doesn't leave).
I'm using the window.onunload event to trigger the unlocking of a
page, but I don't want the page to be unlocked when a postback or a
refresh occurs...I only want it when the user goes to another page or
closes the browser/tab...

Is it possible to achieve this behaviour??

Side note: I'm using ASP.NET 2.0 in the server side.

Regards,
Nuno

Aug 2 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"nrocha" <nu**********@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
Hello,

I've been trying to detect when a user closes a page or navigates away
from it. This is part of a solution for locking pages (that is, if a
user enters a page, no one else can enter it while he doesn't leave).
I'm using the window.onunload event to trigger the unlocking of a
page, but I don't want the page to be unlocked when a postback or a
refresh occurs...I only want it when the user goes to another page or
closes the browser/tab...

Is it possible to achieve this behaviour??

Side note: I'm using ASP.NET 2.0 in the server side.

Regards,
Nuno
Now, I am not sure about how to do this in ASP, being a PHP man myself.
However, with a bit of AJAX behind it, you could make a function that
communicates with the server. If the page is currently being viewed, have
ASP open a file or database and mark the page as being read (say "1"). Once
you are done with the page, use the same function to change the file to read
something (say "0"). In JavaScript, all this would require is the standard
AJAX request, plus a function that submits the data, either 1 or 0, to the
server.

Hope that helps!
Matt

Aug 2 '07 #2

P: n/a
On Aug 2, 2:30 pm, "Matthew White" <mgw...@msn.comwrote:
"nrocha" <nuno.a.ro...@gmail.comwrote in message

news:11*********************@w3g2000hsg.googlegrou ps.com...
Hello,
I've been trying to detect when a user closes a page or navigates away
from it. This is part of a solution for locking pages (that is, if a
user enters a page, no one else can enter it while he doesn't leave).
I'm using the window.onunload event to trigger the unlocking of a
page, but I don't want the page to be unlocked when a postback or a
refresh occurs...I only want it when the user goes to another page or
closes the browser/tab...
Is it possible to achieve this behaviour??
Side note: I'm using ASP.NET 2.0 in the server side.
Regards,
Nuno

Now, I am not sure about how to do this in ASP, being a PHP man myself.
However, with a bit of AJAX behind it, you could make a function that
communicates with the server. If the page is currently being viewed, have
ASP open a file or database and mark the page as being read (say "1"). Once
you are done with the page, use the same function to change the file to read
something (say "0"). In JavaScript, all this would require is the standard
AJAX request, plus a function that submits the data, either 1 or 0, to the
server.

Hope that helps!
Matt
Hi,
all that you say it's true and it's what I'm doing right now. The big
problem is:
Once
you are done with the page, use the same function to change the file to read
something (say "0").
How can I detect this correctly? As I've said, I'm using the
window.onunload event but I don't want to unlock the page if I
continue in it (postback or refresh).
An alternative would be to add code to unlock the page when loading
every one of the other pages, but this way I would have to use
timeouts to force the page to unlock when the browser was closed or
when the user navigated away from my site to another...

I can't find a solution without any drawbacks...

Any sugestions??

Nuno

Aug 2 '07 #3

P: n/a
Matthew White said the following on 8/2/2007 9:30 AM:
"nrocha" <nu**********@gmail.comwrote in message
news:11*********************@w3g2000hsg.googlegrou ps.com...
>Hello,

I've been trying to detect when a user closes a page or navigates away
from it. This is part of a solution for locking pages (that is, if a
user enters a page, no one else can enter it while he doesn't leave).
I'm using the window.onunload event to trigger the unlocking of a
page, but I don't want the page to be unlocked when a postback or a
refresh occurs...I only want it when the user goes to another page or
closes the browser/tab...

Is it possible to achieve this behaviour??

Side note: I'm using ASP.NET 2.0 in the server side.

Regards,
Nuno

Now, I am not sure about how to do this in ASP, being a PHP man myself.
However, with a bit of AJAX behind it, you could make a function that
communicates with the server. If the page is currently being viewed,
have ASP open a file or database and mark the page as being read (say
"1"). Once you are done with the page, use the same function to change
the file to read something (say "0"). In JavaScript, all this would
require is the standard AJAX request, plus a function that submits the
data, either 1 or 0, to the server.
That does nothing for telling you when/if/why the user left. Not even
window.onunload is reliable for that. The best you can do is a Server
Session with a timeout on it. In any event, to be reliable it *has* to
be handled on the server and that would mean posting to an ASP.NET group.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 2 '07 #4

P: n/a
"nrocha" <nu**********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On Aug 2, 2:30 pm, "Matthew White" <mgw...@msn.comwrote:
>"nrocha" <nuno.a.ro...@gmail.comwrote in message

news:11*********************@w3g2000hsg.googlegro ups.com...
Hello,
I've been trying to detect when a user closes a page or navigates away
from it. This is part of a solution for locking pages (that is, if a
user enters a page, no one else can enter it while he doesn't leave).
I'm using the window.onunload event to trigger the unlocking of a
page, but I don't want the page to be unlocked when a postback or a
refresh occurs...I only want it when the user goes to another page or
closes the browser/tab...
Is it possible to achieve this behaviour??
Side note: I'm using ASP.NET 2.0 in the server side.
Regards,
Nuno

Now, I am not sure about how to do this in ASP, being a PHP man myself.
However, with a bit of AJAX behind it, you could make a function that
communicates with the server. If the page is currently being viewed,
have
ASP open a file or database and mark the page as being read (say "1").
Once
you are done with the page, use the same function to change the file to
read
something (say "0"). In JavaScript, all this would require is the
standard
AJAX request, plus a function that submits the data, either 1 or 0, to
the
server.

Hope that helps!
Matt

Hi,
all that you say it's true and it's what I'm doing right now. The big
problem is:
>Once
you are done with the page, use the same function to change the file to
read
something (say "0").
How can I detect this correctly? As I've said, I'm using the
window.onunload event but I don't want to unlock the page if I
continue in it (postback or refresh).
An alternative would be to add code to unlock the page when loading
every one of the other pages, but this way I would have to use
timeouts to force the page to unlock when the browser was closed or
when the user navigated away from my site to another...

I can't find a solution without any drawbacks...

Any sugestions??

Nuno
Sorry, you are right. Instead, try logging the IP. If this IP isn't that
IP, then don't display the page. Once the onunload is triggered, set the
log to denote that the user left the page. Run a cron-job every 30 seconds
that checks the database and the timestamp. If the last person left, say, a
minute ago, release the IP and unlock the page.

Matt

Aug 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.