My problem is a bit more complex than using the following simple JavaScript code:
window.onbeforeunload = function (e) {
return 'Are You Sure?';
};
On an e-commerce web page I would like to remind the user that he has items in the shopping cart so that he can change his mind before
- closing the browser tab/window
- navigating to another domain
The JavaScript method above does not solve my problem because it is evoked even when the user navigates within the domain.
Short:
- User tries to close window -> Show dialog
- User changes http://www.mydomain.com/shoppingcart url to http://www.google.com in the browser’s address bar -> Show dialog
- User navigates to http://www.mydomain.com/checkout with the checkout button or presses the back button in the browser -> Do NOT show the dialog
Sorry there’s no technical solution to your “problem.”
It’s not an accident when a user decides to leave your site, i.e. by typing a new URL, so stopping them to say “Hey, you haven’t checked out yet” is kind of pointless.