Detect window close and navigating away from a page

N

nrocha

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
 
M

Matthew White

nrocha said:
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
 
N

nrocha

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
 
M

Matthew White

nrocha said:
Hi,
all that you say it's true and it's what I'm doing right now. The big
problem is:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top