Skip to content Skip to sidebar Skip to footer

Disable Browser Onunload On Certain Links?

I am working on a Web Hosting panel which has a DNS records manager. I have to use a lot of JavaScript to make it perform almost like a desktop application. I really wanted to sho

Solution 1:

You could remove the event handler when they're clicked:

var safeLinks = ['.test', '.test2', '.test3', '.test4', '.test5'];

functionpromptBeforeClose() {
    return'Are you sure you want to leave?';
}

$(window).on('beforeunload', promptBeforeClose);

$(document).on('click', safeLinks.join(', '), function(e) {
    $(window).off('beforeunload', promptBeforeClose);
});

Also, if these safe links have a common ancestor, use that instead of document as the delegation parent or people will complain. ;)

Solution 2:

use a flag variable.When ever click on the onload then make it as true

$(window).on('beforeunload', function(){
 if(flag != false)
  return'Are you sure you want to leave?';
});

Post a Comment for "Disable Browser Onunload On Certain Links?"