简体   繁体   中英

how to unbind onbeforeunload event on javascript function call and page refresh

Please help me how to unbind onbeforeunload event on javascript function call . Also I want to unbind onbeforeunload on page refresh. Actually I want to give pop up alert on page exit .For this I have successfully unbinded onbeforeunload from form submit button and anchor tags. Kindly help me. The code I have used is

var jQuery = jQuery.noConflict();
jQuery(document).ready(function() {
  jQuery(window).bind("onbeforeunload", function() {
      return confirm("Do you really want to close?")
    }
  );

  jQuery('a').on('click', function() {
    jQuery(window).unbind('onbeforeunload')
  });

  jQuery("form").submit(function() {
    jQuery(window).unbind('onbeforeunload')
  });
});

Thanks

Do some minor change in your code, it will be work

jQuery(document).ready(function () {        
    window.onbeforeunload = function () {
        return confirm("Do you really want to close?")
    };

    jQuery('a').on('click', function () {
        jQuery(window).unbind('onbeforeunload')
    });

    jQuery("form").submit(function () {
        jQuery(window).unbind('onbeforeunload')
    });
});

Check this on jqfiddler, Message pop-up showing on refresh click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM