开发者

javascript/jquery - crossbrowser detection when user is closing the browser?

开发者 https://www.devze.com 2023-03-08 04:04 出处:网络
i\'m looking for a reliable way on how to detect when a user closes the browser/tab in order to display a warning message (i\'m having a shopping c开发者_高级运维art which uses sessions).

i'm looking for a reliable way on how to detect when a user closes the browser/tab in order to display a warning message (i'm having a shopping c开发者_高级运维art which uses sessions).

i've googled and couldn't find a proper solution - window.onunload will display a message every time i'm refreshing the page ..

any ideas? thanks


You can't tell the difference between closing, reloading, back/forward etc.

beforeunload is fired on all of them.

Depending on if you launched the window yourself, you could run your code before calling window.close(), but this won't be called if the user closes the window themselves.


I don't believe such a thing is possible. The browser can fire an event when the page (un)loads, but who's to say wether the user is navigating, or closing the browser/tab?

0

精彩评论

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