Jump to content

Call script on closing a window


nikhilthecool

Recommended Posts

This is not possible with PHP as it is all executed when the page loads therefore to process code when a windows is closed needs to be done via JavaScript but then you have problems with some people disabling javascript and so on..

I wanted to do this a long time ago to create my online stats for an old site but i ended up using datetime in a database and basing it on an action in past 5 minutes as it being an online user then each page they click it updated the datetime of visit..

If you are wanting to log a user out then you could always log a cookie with the session ID in and then on each page look to see if the user has the same session if not then they have oviusly exited all the pages on your site and tryed accessing it again..

Maby a litle more info on exacly what you are trying to acheive :)

Regards
Liam
Guest footballkid4
[!--quoteo(post=359681:date=Mar 29 2006, 07:46 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 29 2006, 07:46 AM) [snapback]359681[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Right, but you guys are missing the point. Javascript cannot detect when a user closes the browser window. You're going to have to use shocker's method of checking update times.
[/quote]
Yes, it can...
[code]<body onUnLoad="function_here()">[/code]
i already tried onUnload()

But the prob here is that, after i close the window, i.e., when onUnload is called, i have to run a script for LOGOUT which works fine on closing the window.

But, this also works when i REFRESH the page. So whenever the user refreshes the page, he gets logged out and relogged in automatically. lol.

i guess onUnload isnt appropriate. But is there any logic wherein in can prevent the user from refreshing a page??
Guest footballkid4
[!--quoteo(post=360004:date=Mar 30 2006, 05:31 AM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Mar 30 2006, 05:31 AM) [snapback]360004[/snapback][/div][div class=\'quotemain\'][!--quotec--]
No, you cannot stop a user from refreshing the page through the browser. And AFAIK, closing the actual browser window does not fire the onUnload() function. Leaving the page will, however, but that doesn't fix your problem.
[/quote]
It does get fired when the user closes the window, I've had to use it in my scripts before. I did forget about the refershing though.

I say go with shocker's idea, the time frame would be best.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.