简体   繁体   English

如何取消对javascript函数调用和页面刷新的onbeforeunload事件的绑定

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

Please help me how to unbind onbeforeunload event on javascript function call . 请帮助我解除javascript函数调用上的onbeforeunload事件的绑定。 Also I want to unbind onbeforeunload on page refresh. 我也想在页面刷新时取消onbeforeunload的绑定。 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. 实际上,我想在页面退出时发出弹出警报。为此,我已经成功地将onbeforeunload与表单的提交按钮和锚点标签解除了绑定。 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 在jqfiddler上检查此内容,刷新时显示消息弹出窗口, 单击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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