Skip to content Skip to sidebar Skip to footer

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:

Solution 1:

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

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

Solution 2:

Yes, there is a way. The onbeforeunload function works a bit differently than other events. All you have to do is return a string from the function, and the browser will do the rest of the work for you. The syntax is like this:

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

And that's all that you need to do. Clicking "Cancel" in the dialog that comes up will keep the user on the current page, and OK will let the user navigate away or close the page. It's really easy enough that you don't need to use jQuery at all.

Solution 3:

$(window).unload(function() {
    var answer = confirm("Leave This website?")
if (answer){
    returnfalse;
}
else{
    alert("Thanks for sticking around!");
    returntrue;
}
});

Post a Comment for "Kill Unload Function In Js?"