Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript onbeforeunload cancel

Is there a way to cancel the javascript event onbeforeunload, and just have it execute a function and return to the page?

window.onbeforeunload = function(){
    //execute some logic
    return false;
}
like image 884
user1817081 Avatar asked Sep 23 '13 20:09

user1817081


2 Answers

No. You cannot cancel this event. All you can do is have it return a string. That will make the browser ask the user if they want to leave.

Just imagine if you could block this event! You could make a site that the user could never close! Just imagine all the popups you could make. There's no way browsers are going to allow this.

like image 198
Rocket Hazmat Avatar answered Sep 20 '22 18:09

Rocket Hazmat


No. You can't stop people leaving a page if they want to.

like image 36
Quentin Avatar answered Sep 20 '22 18:09

Quentin