Check if links are external with jQuery/javascript?

<script type="text/javascript">
var comp = new RegExp(location.hostname);

jQuery('a').each(function(){
 //location.hostname === this.hostname || !this.hostname.length 
   if(comp.test(jQuery(this).attr('href'))){
       // a link that contains the current host           
       jQuery(this).addClass('local');
   }
   else{
       // a link that does not contain the current host
       jQuery(this).attr('rel','nofollow');
   }
});
</script>

Leave a Reply

Your email address will not be published. Required fields are marked *