If you need to rely on JavaScript:
Basically you just need to change your if
-condition (to check whether the anchor link points to an external location or not).
So, instead of if(anchor.getAttribute("href"))
, use if(anchor.getAttribute("href") && anchor.hostname!==location.hostname)
.
With a bit of code clean up, your function should look as follows:
function externalLinks() {
for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
var b = c[a];
b.getAttribute("href") && b.hostname !== location.hostname && (b.target = "_blank")
}
}
;
externalLinks();