javascript onbeforeunload禁用链接 [英] javascript onbeforeunload disable for links

查看:139
本文介绍了javascript onbeforeunload禁用链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助。我正在使用JavaScript,我无法配置如何使用 window.onbeforeunload



这里是我简单的代码:

  window.onbeforeunload = function(){
if(!confirm(some message here )){
返回你确定吗?;
} else {
return false;
}
}

我只需要在关闭浏览器时使用代码,重新加载页面或退出标签菜单。我需要的是,当我按下我的网页正文或页面中的链接时,它也不会在任何链接上工作。你有什么想法或解决方案吗?



我希望你能帮助我解决这个问题。感谢您的好意。



谢谢

解决方案

即:

  $(document).on('mousedown','a [href]',offBeforeUnload)
.on('mouseleave','a [href]',function(){
$(window).on('beforeunload',windowBeforeUnload);
});



函数offBeforeUnload(event){
$(window).off('beforeunload');
}

函数windowBeforeUnload(){
if(!confirm(some message here)){
returnAre you sure?;
} else {
return false;



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

DEMO


I need your help. I'm working with JavaScript and I'm not able to configure how to work on window.onbeforeunload.

here's my simple code:

window.onbeforeunload = function () { 
      if (!confirm("some message here")) { 
          return "Are you sure?"; 
      } else{ 
        return false; 
      } 
}

I just need to work the code in closing the browser, reloading page and or exiting tab menu. I need that it will not also work on any links when I press links in my web body or page. Do you have any idea or solution for this?

I hope you can help me with this. I appreciate your kindness.

Thank you

解决方案

You could try that:

$(document).on('mousedown', 'a[href]', offBeforeUnload)
    .on('mouseleave', 'a[href]', function () {
        $(window).on('beforeunload', windowBeforeUnload);
    });



function offBeforeUnload(event) {
    $(window).off('beforeunload');
}

function windowBeforeUnload() {
     if (!confirm("some message here")) { 
          return "Are you sure?"; 
      } else{ 
        return false; 
      } 
}

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

DEMO

这篇关于javascript onbeforeunload禁用链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆