Does anyone know how to make it so that all external links automatically open in a new tab?
I’m happy for all links to be this way, or ideally internal ones just open in the current tab.
Is there a way to do this that doesn’t require me to tick the box every time?
I think the idea was good, had a similar one with vanilla javascript but it does not work. It looks a tiny bit more complicated, here is the solution I found and pimped up a bit:
Vanilla Javascript
<script>
function externalLinks() {
let arrayOfLinks = document.getElementsByTagName("a");
for (i = 0; i < arrayOfLinks.length; i++) {
let link = arrayOfLinks[i];
link.getAttribute("href") &&
link.hostname !== location.hostname &&
(link.target = "_blank");
}
}
externalLinks();
</script>
no, your script actually does not work I did try your idea but it opens links within the same tab unfortunately. The above scripts appear to be the only way to go.
EDIT:
The magic happens within the location.hostname parameter
I have read an interesting article regarding links being opened with target=“_blank” It appears we should add another attribute called “noopener” which prevents attack surface vulnerabilities.
Sorry I never replied to this, I hadn’t even realised a discussion took place!
Thank you so much!
I will give the code a try.
Is it possible to make it so that only external links open in new tab?