Hi there! I have been trying to add custom code to open all external links to a new tab. Every tutorial I have tried has not worked. The website we are creating for our client is lalani-jennings.myshopify.com.
The link that we most need to open in a new tab is in the footer menu - “Web Design by Balance Design Agency”.
please, try the following code snippet. It’s a JS code that will open all external links to a new tab.
var links = document.links;
for (let i = 0, linksLength = links.length ; i < linksLength ; i++) {
if (links[i].hostname !== window.location.hostname) {
links[i].target = '_blank';
}
}
You need to paste the code in the main JS file (not the main liquid file)! The file name may vary depending on the theme that you’re using – e.g. theme.js
The article describes something very similar to what you want to set up (the only difference is the attribute you want to add to all external links). Maybe this could also help you?
Hope this helps! If you have any questions, let me know.