How to prevent the user from leaving the page
Posted
by JohnathanKong
on Stack Overflow
See other posts from Stack Overflow
or by JohnathanKong
Published on 2010-05-25T14:57:56Z
Indexed on
2010/05/25
15:01 UTC
Read the original article
Hit count: 213
JavaScript
|javascript-events
Hey Everyone,
I am currently building a registration site where if the user leaves, I want to pop up a CSS box asking him if he is sure or not. I can accomplish this feat using confirm boxes, but the client says that they are too ugly. I've tried using unload and beforeunload, but both cannot stop the page from being redirected. Using those to events, I return false, so maybe there's a way to cancel other than returning false?
Another solution that I've had was redirecting them to another page that has my popup, but the problem with that is that if they do want to leave the page, and it wasn't a mistake, they lose the page they were originally trying to go to. If I was a user, that would irritate me.
The last solution was real popup window. The only thing I don't like about that is that the main winow will have their destination page while the pop will have my page. In my opinion it looks disjoint. On top of that, I'd be worried about popup blockers.
© Stack Overflow or respective owner