2017-01-25 32 views
0

我使用此代码来通知用户他们将要去第三方网站,但我想排除一些外部URL。如何/在哪里可以在此代码中执行此操作?需要排除一些外部链接接收此通知

<script> 
    $('a').each(function() { 
     if (this.href !== "#" && this.href.indexOf('/') !== 0 && this.href.indexOf(location.hostname) === -1) { 
     $(this).attr('target', '_blank') 
      .click(function() {  return confirm('NOTICE: You are leaving our website and will enter a website maintained by a third party. We are providing a link to the third party website solely as a convenience to you, because we believe that website may provide useful content. We are not, by referring or linking to the third party website, incorporating its contents into our own website. We do not endorse or guarantee, and we disclaim any responsibility for: the content, products or services offered on that website, its performance or interaction with your computer, its security and privacy policies and practices, and any consequences that may result from visiting that website. By clicking OK, you acknowledge this statement.'); 
     }); 
     } 
    }); 
}); 
</script> 

回答

0

您需要检查该网站是否在白名单内。因此,您需要创建一些内容以允许您检查网站列表。

我也建议您将逻辑提取到一个函数中,以使其更容易维护。

function IsNoticeRequired(url) 
    {  
     // Makes dealing with exclusions easier. 
     url = url.toLowerCase(); 

     // Put the white listed sites here. 
     var excludedSites = 
     [ 
      "stackoverflow.com"  
     ]; 

     // Determine if the site is in the exclusion list. 
     var isInExclusion = false; 
     for(var i = 0; i < excludedSites.length; i++) 
     { 
      // Need to check if url contains site. 
      if(url.indexOf(excludedSites[i]) !== -1) 
      { 
       isInExclusion = true; 
       break; 
      } 
     } 

     var isNoticeRequired = url !== "#"; 
     isNoticeRequired = isNoticeRequired && url.indexOf("/") !== 0 
     isNoticeRequired = isNoticeRequired && url.indexOf(location.hostname) === -1; 
     isNoticeRequired = isNoticeRequired && !isInExclusion; 

     return isNoticeRequired; 
    } 

$('a').each(function() { 
    if (IsNoticeRequired(this.href)) { 
    $(this).attr('target', '_blank') 
     .click(function() {  return confirm('NOTICE: You are leaving our website and will enter a website maintained by a third party. We are providing a link to the third party website solely as a convenience to you, because we believe that website may provide useful content. We are not, by referring or linking to the third party website, incorporating its contents into our own website. We do not endorse or guarantee, and we disclaim any responsibility for: the content, products or services offered on that website, its performance or interaction with your computer, its security and privacy policies and practices, and any consequences that may result from visiting that website. By clicking OK, you acknowledge this statement.'); 
    }); 
    } 
}); 

它的jsfiddle在行动:https://jsfiddle.net/9n7v67xa/

+0

我换成stackflow.com以上,我想从通知中排除的URL,全部换成了以前的代码本,但它不工作我。现在一切都没有通知打开。谢谢你的努力! – TDB

+0

@TDB代码示例中存在小问题。 – Brian

+0

完美!非常感谢你!!! – TDB