Open external links in a new tab-Debut theme

Excursionist
26 0 4

Hello,

I need to open external links for some clicks in my home page.

I searched in Shopify topics and I found this code:

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';
  }
}

How can I customized it to make it work for (Instagram click) for example ?

What should I write inside the code ?

My website URL: https://presets-market.com/

0 Likes
Explorer
72 10 10

You can add class called 'external' inside liquid files of 'a' href tag.
Than you need to add following code in your file. I just update your code.

var links = document.querySelectorAll(".external");
for (let i = 0, linksLength = links.length ; i < linksLength ; i++) {
    links[i].target = '_blank';
    links[i].rel = 'noreferrer noopener';
}

 

Still not satisfied!!! contact me
Is it good solution? Yes, please Like.
0 Likes