开发者

Do something before closing the browser window

开发者 https://www.devze.com 2022-12-30 04:23 出处:网络
we have an web-application built in flash (it\'s actually just getting built :D) where is very important to be notified, when user closes his window.

we have an web-application built in flash (it's actually just getting built :D) where is very important to be notified, when user closes his window.

Since it's in flash, we do not worry about Javas开发者_JAVA技巧cript, we do not support non-javascript users.

What is the safest (meaning it's 100% sure it gets called) X-browser way to call php script to close session, make some db changes, etc.?

Thank you


There is no method which is 100% sure to work. You can't get a signal form the client if there is a network problem, power cut, etc.

Periodically run a clean up script (based on time, not activity) that performs all the deletions etc for sessions which have had no activity in X minutes.


The onbeforeunload and/or onunload events are probably what you are looking for. You can probably fire off an ajax call there but it could be possible that it gets interrupted when the page is actually onloaded.

If detecting users leaving via a link is enough, simply fire off an ajax request in $('a[href]').click(...);


There is not way to ensure that. The closest you can get is doing something in the onunload or beforeunload events.


When using jQuery you can use the example posted here Call URL before closing of Browser Window

The basic idea of this to make an ajax request when the window/tab is being closed.


There is no 100%, sure-fire way to detect this. It may be possible (others can perhaps elaborate on this) to perform some AJAX call on the onunload event, which you can use to perform some clean-up, but you cannot rely on this. Browsers may crash, or exhibit unsuspected behavior. Or users may simply not close the browser for a very long time.

0

精彩评论

暂无评论...
验证码 换一张
取 消