我编写此脚本是为了在用户单击我们网站的外部链接时弹出一条消息。当我写这篇文章时,我认为最好的方法是检查 location.host 并将其与用户尝试访问的 url 进行比较。
<script type="text/javascript">
$(function(){
$('a').click(function(){
if (this.href.match(location.host)) {
//alert('Please continue on to our site.');
window.onbeforeunload = null;
}
else {
if (window.confirm('NOTICE: By accessing this link, you will be leaving the DBPR website. DBPR is not responsible for the content of the Internet website you are entering. DBPR neither warrants nor makes any representations nor endorsements as to the accuracy, quality, content or completeness of the information, text, images, graphics, hyperlinks, and other items contained on the Internet website you are entering. DBPR is not responsible or liable for any viruses or contaminations of your hardware, software, peripherals or property, resulting from use of the Internet websites linked to or from the DBPR Internet website. Do you want to proceed?')
){
// They clicked Yes
}
else
{
// They clicked no
return false;
}
}
});
});
</script>
就代码目前的情况而言,它在大多数情况下都有效,但是我注意到我们主页上的几个按钮引用了 javascript:void(0),并且它们导致确认框在单击时提示。
您是否建议将 javascript:void(0) 视为内部链接或完全忽略它?
谢谢, TG
最佳答案
你可以这样做:
if (this.href.match(location.host) ||
this.href.toLowerCase().indexOf('javascript') !== -1) {
// allow
}
关于javascript - 如何捕获Javascript :void(0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26487522/