外部链接通知 - JavaScript或JQuery

时间:2014-02-03 23:32:56

标签: javascript hyperlink external

我正在寻找一种方法进行设置,以便在点击外部链接时,它会警告人们他们要离开网站。优选地,它会使屏幕变暗并在屏幕中间的框中显示消息,并且可以选择单击“确定”或“取消”。

我尝试使用此代码:

  $("a.external").click(function () {
      alert("You are about to proceed to an external website.  The Great Western Market has no control over the content of this site.  Click OK to proceed.");
  });

并为每个链接提供一个外部类但它似乎不起作用。我不想使用它,因为这意味着客户端必须记住添加我更喜欢更自动的类。

我也尝试使用此代码,但无济于事:

 $('a').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
      })
      .click(function () {
          var x=window.confirm('You are about to proceed to an external website.  The Great Western Market has no control over the content of this site.  Click OK to proceed.');
            var val = false;
            if (x)
                val = true;
            else
                val = false;
            return val;
        });

我正在使用WordPress 3.8.1。

提前感谢您提供任何帮助。

4 个答案:

答案 0 :(得分:3)

您的过滤器逻辑应该是正确的,尝试使用confirm函数,并使用jQuery而不是$。

jQuery('a').filter(function() {
    return this.hostname && this.hostname !== location.hostname;
  }).click(function(e) {
       if(!confirm("You are about to proceed to an external website."))
       {
            // if user clicks 'no' then dont proceed to link.
            e.preventDefault();
       };
  });

我在您网站上的开发工具中尝试了这一点,如果您使用jQuery,它似乎正常工作。我想你可能有一些导致与$冲突的插件。

JSFiddle Demo

答案 1 :(得分:0)

尝试使用confirm代替警报,因为它会暂停并等待用户输入。然后,您需要function(e){ e.preventDefault(); }来阻止默认链接操作。

答案 2 :(得分:0)

要识别外部链接,您可以执行以下操作:

var ignoreClick = false;

$(document).ready( function(){

    $('input[type="submit"], input[type="image"], input[type="button"], button').click(function(e) {
        ignoreClick = true;
    });

    $(document).click(function(e) {
        if($(e.target).is('a')) 
            checkLink(e);
    });

    $('a').click(function(e) {
        checkLink(e);
        return true;
    });

    checkLink = function(e){
        // bubble click up to anchor tag
        tempTarget = e.target;
        while (!$(tempTarget).is('a') && !!tempTarget.parentElement) {
            tempTarget = tempTarget.parentElement;
        } 

        if ($(tempTarget).is('a')){

            if(!!tempTarget && $(tempTarget).is('a') && 
                (tempTarget.hostname == "" || tempTarget.hostname == "#" || tempTarget.hostname == location.hostname)){
                    ignoreClick = true;
            }

        }
    }

});

并抓住可能使用beforeunloadconfirm选项

的消息的人
$(window).bind("beforeunload", function (e) {
     if (!ignoreClick){
         if(!confirm("Leaving website message")) {
             e.preventDefault();
         }
     }
});

答案 3 :(得分:0)

对我来说效果很好。我刚删除了不必要的变量,但原始脚本工作正常。

$('a').filter(function() {
    return this.hostname && this.hostname !== location.hostname;
})
.click(function () {
    return window.confirm('You are about to proceed to an external website.  The Great Western Market has no control over the content of this site.  Click OK to proceed.');
});

http://jsfiddle.net/3dkAN/1/

修改

关注@ user1308743的行,似乎在cgmp.framework.min.js中召唤jQuery.noConflict()模式,取消绑定jQuery的$()函数。因此,请将jQuery()用于任何jQuery实现