外部链接通知-JavaScript或jQuery



我正在寻找一种设置它的方法,以便单击外部链接时,它会警告人们他们要离开网站。最好是,它会使屏幕变暗,并在盒子中间在屏幕中间显示一条消息,并可以单击"确定"或"取消"。

我尝试使用此代码:

  $("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。

事先感谢您提供的任何帮助。

您的过滤器逻辑应该是正确的,尝试使用确认功能,然后使用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

尝试使用confirm而不是警报,因为这将暂停并等待用户输入。然后,您需要function(e){ e.preventDefault(); }来防止默认链接操作。

要识别您可能会执行类似的事情的外部链接:

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();
         }
     }
});

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

$('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实施

最新更新