javascript - 如何捕获Javascript :void(0)

标签 javascript jquery function window void

我编写此脚本是为了在用户单击我们网站的外部链接时弹出一条消息。当我写这篇文章时,我认为最好的方法是检查 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/

相关文章:

javascript - 如何将两个div合并为一个div

c - 新手 C 程序员对错误感到好奇

javascript - javascript中的 Accordion 选择器

javascript - 模态关闭/打开

javascript - 如何避免重复调用javascript函数

javascript - 如何在 jQuery 中调整滚动速度

javascript - jQuery Magnific Popup 根本不起作用

javascript - Dragula:如何使对象跟随光标

jQuery qTip 只出现一次

c - 使用全局变量作为函数的参数