How To Make All External Links Nofollow Using JavaScript

Creating a webpage with proper uses of internal and external links is the oldest and most used way of Search Engine Optimization(SEO). You can create the links in your webpage in two ways using "nofollow" tag, which tell the search engine and crawlers not to follow and crawl your hyperlinks and without using "nofollow" tag, which allows the search engine and crawlers to follow and crawl your hyperlinks.

The rel="nofollow" attribute helps to control the flow of page rank from one website to another and maintain the pagerank of your website. You can create a link with rel="nofollow" attribute as the following. 

<a href="https://www.google.com" rel="nofollow">My Link</a>

In this post, I am going to describe different ways of making all the external links of your webpage "nofollow" using simple JavaScript code and Jquery plugin which helps to prevent following SEO juice form your website to the external links.

You May Also Like:

Making All External Links Nofollow Using Simple JavaScript Code


You can make all of your external links with nofollow tag using the following simple JavaScript code. It adds  rel="nofollow" attribute along with target attribute to "_blank" and title attribute to "Click to open in new window" on <a href="#">Link</a> tag of all of the external links of your website
 <script type='text/javascript'>
function myFunction() {
    var x = document.getElementsByTagName("a");
    var i;
    for (i = 0; i < x.length; i++) {
      if (location.hostname!=x[i].hostname){
        x[i].rel = "nofollow";
        x[i].target = "_blank";
        x[i].title = "Click to open in new window";

}}}

mft=setTimeout("myFunction()",0);

function LoadEvent(func){

var oldonload = window.onload;

if (typeof window.onload != 'function'){
window.onload = func;
}
else{
window.onload = function()
{
if(oldonload)
{oldonload();}
func();}}} 
LoadEvent(function(){
myFunction();
});
</script>


Making All External Links Nofollow Using jQuery


You can also make all of your external links with nofollow tag using the following jQuery code. This code also adds  rel="nofollow" attribute along with target attribute to "_blank" and title attribute to "Click to open in new window" on <a href="#">Link</a> tag of all of the external links of your website

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