Search for anchors with rel attribute and external value

I am looking for jQuery translation from the following:

function externalLinks() {   
 if (!document.getElementsByTagName) return;   
 var anchors = document.getElementsByTagName("a");   
 for (var i=0; i<anchors.length; i++) {   
   var anchor = anchors[i];   
   if (anchor.getAttribute("href") &&   
       anchor.getAttribute("rel") == "external")   
     anchor.target = "_blank";   
 }   
}   
window.onload = externalLinks;

taken from the following site:
http://articles.sitepoint.com/article/standards-compliant-world/3

I started with something similar, but I don’t know how to look for the rel attribute (without scrolling through all the bindings):

$(document).ready(function () {
    $('body').find('a').findTheRelTagWithExternalValue;
});

Thanks
Red

+3
source share
1 answer

Sort of:

$('a[href][rel=external]').attr('target', '_blank');
+7
source

Source: https://habr.com/ru/post/1769628/


All Articles