2017-09-26 54 views
1

我有这个提醒用户,当他们退出到第三方网站的jQuery脚本。只需点击它即可正常工作,但如果用户按Ctrl +单击或右键单击>打开新选项卡,则不会显示警告消息。如何修改此代码以使通知显示,无论用户点击/打开链接的方式如何?离开网站通知

// notification when exiting to third party website 
jQuery('a').filter(function() { 
    return this.hostname && this.hostname !== location.hostname; 
}).click(function(e) { 
if(!confirm("You are now leaving....")) 
    { 
     // return back to page on no. 
     e.preventDefault(); 
    }; 
}); 

这是一个信用社监管,所以我不这样做的一个布特海德。

+1

这是一个相当苏茨基的事情,您的访客。 – ceejayoz

+1

我经常按Ctrl +点击绕过该警告。更好地重定向到一个普通的页面,并从那里重定向(如Facebook一样) –

+0

优秀的输入。它是信用社的规定要求,所以不是我的选择。 – xxdash

回答

1

右键单击是上下文菜单是浏览器功能。您必须完全禁用它或使用HTML创建您自己的上下文菜单。

这里是一个解决问题的答案ctrl +点击。

如果键被按下,如果它是Ctrl键,只需添加e.preventDefault()

$(document).keydown(function (e) { 
 
    if(e.which==17) 
 
     e.preventDefault(); 
 
}); 
 

 

 
jQuery('a').filter(function() { 
 
    return this.hostname && this.hostname !== location.hostname; 
 
}).click(function(e) { 
 
if(!confirm("You are now leaving....")) 
 
    { 
 
     e.preventDefault(); 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<a href="https://www.google.com">G</a>

+0

谢谢!奇迹般有效。 – xxdash