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;
}
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;
}
Share
Improve this question
edited Mar 5, 2015 at 23:32
Platinum Azure
46.2k13 gold badges111 silver badges135 bronze badges
asked Sep 23, 2013 at 20:58
user1817081user1817081
1,1933 gold badges15 silver badges21 bronze badges
2
- 6 Sure. You can write your own web browser that has this feature. – Mike Christensen Commented Sep 23, 2013 at 21:01
- 2 I think you forgot to put quotation marks around "feature." – Michael Calvin Commented Feb 25, 2014 at 21:22
3 Answers
Reset to default 13No. 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.
No. You can't stop people leaving a page if they want to.
Yes. You can do this by making it null.
window.onbeforeunload = null;