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

Detecting close button through X and ALT-F4 combination

P: 82
Hi,

I want to detect when the user close a window through the X button or ALT-F4 or
when the user go to another page and leave my page. I try to use the onuload and on unloadbeore
but it works on ie 6 and Firefox 2 and it doesn't work on Safari 3.03 and Opera 9.2, it is also doesn't solve the problem when the user navigate to another page through the same windows, Any Ideas?

Thanks
Ronen
Jul 2 '08 #1
Share this Question
Share on Google+
9 Replies


acoder
Expert Mod 15k+
P: 16,027
I assume you mean onbeforeunload. That's supported in Firefox and IE, not sure about Safari. Opera doesn't require it unless you're closing the browser because the scripts remain running when you go back to the page if you only navigate away with back/forward. There's no reliable cross-browser way to differentiate between navigating away and the close event. Why do you need to detect it?
Jul 3 '08 #2

P: 82
Hi Acoder

Nice to hear from you again. The reason that I need it is because i want to close some records in the DB that indicate when the user leave the site. I tried the onbeforeunload but it did not solve the problem when the user navigate to a new windows on opera or safari. This is why I need it.

Another question can I have an event listener in the background that can catch key
sroke like ALT+F4.

I did soem research and found that ie6 and FF2 master the onunload and onbeforeunload completly. While opera 9 doesn't support it at all an safari 3 support only the navigating action and not the close(x) and ALT-F4.
Jul 3 '08 #3

acoder
Expert Mod 15k+
P: 16,027
What if the user accidentally went back one page (can be triggered by the backspace key)? I would use a timeout instead. After a period of inactivity, the user is automatically signed out, i.e. if he returns to your site, you can compare the timestamp and require the user to sign in again.
Jul 3 '08 #4

P: 82
What if the user accidentally went back one page (can be triggered by the backspace key)? I would use a timeout instead. After a period of inactivity, the user is automatically signed out, i.e. if he returns to your site, you can compare the timestamp and require the user to sign in again.
It can be done if you run a cron job that sign-out users automatically after T minutes. You must keep in mind that the user can click the back button and never return to the site again this is also possibility.
Jul 3 '08 #5

acoder
Expert Mod 15k+
P: 16,027
True, though you don't need to worry about that if no-one visits again. It depends on what you're storing in the database for the user.

onbeforeunload should really only be used as a convenience, not as an annoyance.
Jul 3 '08 #6

P: 82
Thanks acorder for you help.
Jul 4 '08 #7

acoder
Expert Mod 15k+
P: 16,027
No problem. Did you manage to solve your problem?
Jul 4 '08 #8

P: 82
No problem. Did you manage to solve your problem?
Not really, I have another problem, maybe you know the solution. How can I detected which link was pressed in the unload function before jumping to this link.

I have an unload function that doing some actions. I want this unload function to be be activated only if the link that we jump to is not in some links list.
Any suggestions?
Jul 6 '08 #9

acoder
Expert Mod 15k+
P: 16,027
You can't detect which URL the user will navigate to. That would be a lack of security/privacy and could be misused.

However, if it's via clicking a link on your site, you can add some code onclick for the links that match those that you have in the links array/list.
Jul 7 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.