Ebenezer Obasi

~ Web Dev & Designer ~

This code will add a nofollow attribute to external pages on your website
Posted On Saturday, September 27, 2014

I found out that Spammers has turned one my website into their hub. So, I came up with this code to discourage them from posting on my site and also remain on the good side of Google. The code will automatically attribute a rel="nofollow" to all the external anchor tags on your website.



<script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
$(document).ready(function() {
  $("a[href^='http://']").each(
    function(){
     if(this.href.indexOf(location.hostname) == -1) {
        $(this).attr('rel', 'nofollow');
      }
    }
  );
$("a[href^='https://']").each(
function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr('rel', 'nofollow');
}
}
);

});
</script>

Simply paste the code on the <head></head> section of your website.

Email Newsletter

Like what you read here in this blog post?
Get more like it delivered to your inbox daily.


Contact Form

Name

Email *

Message *