简体   繁体   中英

detecting browser close all conditions

This question has been asked many times before but none of the previous ones hit all the issues. I have a shopping cart that I store in session state. I'd like to detect when the user leaves the site so I can ask if they'd like to save their cart. I'd prefer not to automatically store the cart, I hate sites that I re-visit and there's all this leftover stuff in the cart that I'm no longer interested in.

I've tried onbeforeunload but it fires when the user hits the back/previous buttons. Is there any way from within onbeforeunload I can determine the url where the browser will be going next? Then I can check if it is local and ignore it, otherwise if leaving the site for good a prompt can be displayed.

Maybe I am not getting something? How do you handle this situation?

How do you handle this situation?

You don't.

Use a session timeout to clear the shopping cart.

Besides, what if the user accidentally closes the site? Their cart would automatically be cleared, which makes for terrible UX.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM