Add a global rel=”noopener noreferrer” with jQuery

Add this jQuery script globally to add a rel=”noopener noreferrer” attribute to all your target=”_blank” links. Ok so, you may have a lot of pages with lots of links, maybe over 100 pages with links going all over the web. And you don’t feel like going through all the pages and checking and double checking all your link tags. Simply use the script below into one of your global javascript file to add the noopener and noreferrer rel attribute to all of your pages!

$(document).ready(function(){
	$('a').each(function(){
		if ( $(this).attr('target') == '_blank'){
			$(this).attr('rel','noopener noreferrer');
		}
	})

});

Note: Don’t forget to in include the jQuery script.

So, what the script will do is when the page loads, it will search the page for all the ‘a’ tags that has the “_blank” target attribute. Then it will add the attribute rel=”noopener noreferrer” to links.
In case you didn’t know about this potential security risk, please see links below:

Leave a comment

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

five × 4 =