How to open the links in the site in new tab .

How to open the links in the site in new tab .

Not applicable

Hi There ,

I have recently added this code to open links in the site to open in new tab  and it is working in desktop  but  when I click in the recommended products or recently viewed products in after clicking any specific products it is not opening in new tab , Any idea on how to solve this issue . 

Site URL :: https://6v2hzrj6oxibj021-71493648693.shopifypreview.com

if (window.innerWidth > 768) {
  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';
    }
  }
}
Replies 3 (3)

RockTechnolabs
Shopify Partner
251 36 60

Hi @Anonymous Can you please provide me collaborated access so I can help you batter. 

banned
trumpmr97
Visitor
3 0 0
  1. Use the mouse: To open a link in a new tab using the mouse, simply hover over the link and right-click. Then, select "Open link in new tab" from the menu.

trumpmr97
Visitor
3 0 0

The code you have provided is a JavaScript snippet that opens links in a new tab on desktop devices only. The snippet works by first checking if the width of the window is greater than 768 pixels. If it is, then the snippet loops through all of the links on the page and adds the target="_blank" attribute to them. The target="_blank" attribute tells the browser to open the link in a new tab.

The snippet also adds the rel="noreferrer noopener" attribute to the links. This attribute tells the browser to prevent the website from tracking the user when they click on the link.

Here is a breakdown of the code:

if (window.innerWidth > 768) {

This line of code checks if the width of the window is greater than 768 pixels. If it is, then the snippet will execute the code inside the curly braces.

for (let i = 0, linksLength = links.length; i < linksLength; i++) {

This line of code loops through all of the links on the page. The variable i is used to keep track of the current link. The variable linksLength is the total number of links on the page.

if (links[i].hostname === window.location.hostname) {

This line of code checks if the hostname of the link is the same as the hostname of the current page. If it is, then the snippet will add the target="_blank" and rel="noreferrer noopener" attributes to the link.

links[i].target = '_blank';
links[i].rel = 'noreferrer noopener';

These lines of code add the target="_blank" and rel="noreferrer noopener" attributes to the link.