I want all external links on my Shopify site to open in a new window. Should be simple. I see recommendations to input this code at the end of Theme.js (I’m using Flow theme, version 39.1.0):
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’;
links[i].rel = ‘noreferrer noopener’;
}
}
I did that. It didn’t work. I opened the website in an Incognito browser; none of the links open in a new window. I had someone else try, who actually knows what they’re doing with coding. It didn’t work for him either. Please be kind, I know nothing about coding and this is my first post in the community.
So it may be your specific attempt or issues with the theme, etc etc.
Contact me if you need this customization or theme repair services.
Contact info in forum signature.
ALWAYS please provide context, examples: store url, theme name, post url(s) , or any further detail in ALL correspondence.
Thank you, I tried that and still nothing happened. I put the code at the bottom of the Theme.js, as that’s where the Shopify Help Bot told me to put it. If there is somewhere else I should put it, how would I know? To be clear, my IN-TEXT links open in a new window as they should. It’s the BUTTONs that are not opening in new windows. For example, if you go to the page https://coldcurrentkelp.com/pages/press, and click “Watch Here” it won’t open in a new window - because that “Watch Here” link is in a BUTTON. (But, for example, the in-text links under the Azure Rd. graphic open correctly in new windows because they are IN-TEXT hyperlinks.) Not sure what I’m doing wrong!
Thank you for clarifying! Since the issue is specific to links within buttons and not regular text links, the JavaScript code needs to target button elements that contain external links as well. We need to target buttons that contain anchor () tags and apply the same logic to open them in a new window.