2017-01-25 5 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

PERFECT !!! Огромное спасибо!!! – TDB