我写这个脚本是为了在用户点击我们网站的外部链接时弹出一条消息。当我写这篇文章时,我认为最好的方法是检查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
答案 0 :(得分:0)
你可以这样做:
if (this.href.match(location.host) ||
this.href.toLowerCase().indexOf('javascript') !== -1) {
// allow
}