Fuxi
Fuxi

Reputation: 7599

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

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 cart 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

Upvotes: 0

Views: 656

Answers (2)

dkruythoff
dkruythoff

Reputation: 361

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?

Upvotes: 1

alex
alex

Reputation: 490647

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.

Upvotes: 4

Related Questions