Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill unload function in JS?

Is there a way to kill the unload function with javascript(jquery)?

I am looking for something like this:

window.onbeforeunload = function(){
    confirm("Close?")
}

or in jquery:

$(window).unload(function() {
    confirm("close?")
});

Now, on window unload I get my confirm alert but it will continue in any case. Clicking cancel it won't stay on my page.

like image 522
Newbie Avatar asked May 21 '26 21:05

Newbie


1 Answers

the function has to return false to abort or true to continue, so you cauld simply return the confirm like this:

window.onbeforeunload = function(){
   return confirm("Close?")
}
like image 157
oezi Avatar answered May 24 '26 11:05

oezi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!