window.onbeforeunload doesn't always fire?

J

Jason

Hello everyone,
I have a chatroom. If someone leaves, I need to tell the server that
person has left. Currently i catch window.onbeforeunload to check if
the person tries to leave. I noticed that when I don't have a dialog
popup asking if the person really wants to leave, my Ajax-request code
doesn't always get sent to the server. Basically, the popup ensures my
beforeunload code gets executed. Why is this?

Also, when I try to stop the event, it will popup a dialog box that I
didn't make with the text "Are you sure you want to navigate away from
the page?". Is this a firefox/IE thing? Is there anyway to change the
text on the dialog box?

Thanks!
Jason Ho
 
V

VK

Jason said:
Also, when I try to stop the event, it will popup a dialog box that I
didn't make with the text "Are you sure you want to navigate away from
the page?". Is this a firefox/IE thing? Is there anyway to change the
text on the dialog box?

You can *add* (not change) your own text below the default one in IE's
alert box. You cannot alter the alert box on other UA's AFAIK.

Also you seem to have a wrong idea of what onbeforeunload is doing. You
cannot seamlessly prevent navigation away from the current page against
visitor's will. Just think what would happen on the Web if it was
possible. The maximum you can do with onbeforeunload is to nag users
with "last chance to change your mind" alerts. You don't make these
alerts and you are not in power to cancel them. You trig them
automatically when returning false from your handler (thus overriding
the default action).
 
J

Jason

So what would be the best way to do some processing when the user
leaves/disconnects/closes the browser? Is it not a client-side
solution?

I was thinking I could make the server check every X seconds whether it
got a poll message from the client. If it didn't, it would log the user
out. Is this the most graceful solution, or is there something better I
can do? How does Gmail detect immediately when someone leaves the page
or closes the browser?

- Jason
 

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
474,472
Messages
2,571,832
Members
48,802
Latest member
shadowoftheunknown
Top