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 to make all external links no-follow 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:
- How to create a Simple calculator Using HTML and JavaScript
- How To Create Simple Image Slideshow Using JavaScript
Table of Contents
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 external links no-follow 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='https://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>
1 Comment
Many thanks for sharing a useful and guide for nofollow links…I will try to implement it, hoping it will me to save precious time to make them nofollow manually!