How to open the links in the site in new tab

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.