How to Open External Links in a New Tab on Shopify
Shopify themes ship with almost no JavaScript by default. That is normally a good thing. It keeps page speed high. The downside is that simple front-end behaviors you take for granted on WordPress are not built in. Opening external links in a new tab is one of them.
Here is the snippet I drop into every Shopify build.
The Code
Open your theme's main JavaScript asset file. On Dawn and most modern Shopify themes, that is assets/global.js. Paste this at the bottom of the file:
(function () {
const links = document.links;
for (let i = 0; i < links.length; i++) {
if (links[i].hostname !== window.location.hostname) {
links[i].target = '_blank';
links[i].rel = 'noreferrer noopener';
}
}
})();
Save the file. Reload your storefront. Every link pointing to a different domain now opens in a new tab.
What It Does
The snippet walks every link on the page. If the link's hostname does not match your store's hostname, it sets two attributes:
target="_blank"opens the link in a new browser tab.rel="noreferrer noopener"prevents the new tab from referencing your store and patches a small security issue withtarget="_blank".
Where to Paste It Per Theme
- Dawn, Sense, Refresh:
assets/global.js - Older modern themes:
assets/theme.jsorassets/theme.js.liquid - Heavily customized themes: any custom JS file that runs on every page
If your theme uses a build step (rare on Shopify), paste it into your source file and rebuild. If not, the change is live on save.
Why This Matters for SEO and UX
External links to YouTube reviews, supplier sites, or trust badges send visitors away. If they open in the same tab, most of those visitors are gone for good. Opening them in a new tab keeps your store one click away.
On the SEO side, the rel="noreferrer noopener" part is a small win for security. Without it, the new tab can manipulate the original page via window.opener. Modern browsers default to safer behavior for _blank now, but being explicit is good hygiene.
If you are running a Shopify store and want a tighter take on the platform's SEO trade-offs, I wrote about that in Is Shopify Bad for SEO?.
More notes
The Best AI Image Generation Workflow I've Found (Gemini + Canva)
My best AI image generation workflow uses Gemini to create the image and Canva Magic Layers to clean it up. Here's the exact process I use to get images that actually look good on a page.
AI ToolsGemini vs ChatGPT: An Honest Review After Daily Use
My honest Gemini vs ChatGPT review after using both every day. Gemini wins on images, memory, and cost. ChatGPT is less lazy on long tasks. Here's who actually wins.
