【发布时间】:2014-10-21 13:07:44
【问题描述】:
我编写此脚本是为了在用户单击我们网站的外部链接时弹出一条消息。当我写这篇文章时,我认为最好的方法是检查 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
【问题讨论】:
-
你能举一个链接的例子吗?
-
...
|| this.href.match(/^javascript:/i)? -
为什么要使用
javascript:void(0)(应该是void 0)?为什么听众在不需要它的链接上?附加侦听器时测试链接,仅将其添加到外部链接。如果外部链接被认为是有害的,为什么用户在点击外部链接之前不知道它们? -
我不完全确定为什么使用 javascript:void(0)。我正在开发一个最初于几年前开发的网站,我只是想为其添加更多功能,直到我们的新网站上线。感谢您的所有建议
标签: javascript jquery function window void