Here's the snippet of HTML that can be used to call this file:
<script src="/js/opening-external-links-in-new-tabs.js" type="text/javascript"></script>
And here's the JS file opening-external-links-in-new-tabs.js
:
var links = document.getElementsByTagName("a");
for (counter = 0; counter < links.length; counter++) {
var link = links[counter];
if (link.target == "_self") {
link.getAttribute("href") && link.hostname !== location.hostname && (link.target = "_self");
} else {
link.getAttribute("href") && link.hostname !== location.hostname && (link.target = "_blank");
};
};
To open an EXTERNAL link in the CURRENT tab, write a link like this:
<a href="https://github.com" target="_self">GitHub</a>
To open an INTERNAL link in a NEW tab, write a link like this:
<a href="https://example.com" target="_blank">My website</a>