How to prevent product links from opening in new tabs on mobile version?

Hello There ,

In my website I have made the modifications such that when clicked on all the products on the website it opens in the new tab , what should i do so that this occurs in the desktop version of website and not in the mobile version .

PS : I added this code to stop the menu from opening in new tab when clicked in mobile , what modifications should be made to stop opening products in new tab .

<script>
window.addEventListener("load", function () {
function handleAnchorTags(selector) {
const menuLinks = document.querySelectorAll(selector);

// Loop through the anchor tags
menuLinks.forEach(function (link) {
// Check if the anchor tag has the target="_blank" attribute
if (link.getAttribute("target") === "_blank") {
// Remove the target="_blank" attribute
link.removeAttribute("target");

// Add a click event listener to open the link in the same tab
link.addEventListener("click", function () {
// Get the link's href attribute
const href = link.getAttribute("href");

// Redirect to the link's href in the same tab
window.location.href = href;
});
}
});
}

// Call the function with different selectors
handleAnchorTags(".offcanvas__menu_ul .offcanvas__sub_menu_item");
handleAnchorTags(".offcanvas__menu_ul .offcanvas__menu_item");
});
</script>

Site url :: https://1ajhtj8ajh4a5fcv-71493648693.shopifypreview.com

Thank You!!

Hi @Anonymous Please check the below screenshot

From your menu anchor tag remove the target=“_blank” attribute

Or else add this


Not working!!!

Okay Just add this @Anonymous in the script tag

var link = document.querySelector("a.d-block");

// Check if the link has the target="_blank" attribute
if (link.getAttribute("target") === "_blank") {
    // If it has the attribute, remove it
    link.removeAttribute("target");
}

it is still opening in new tab

Follow these Steps:

  1. Go to the Online Store
  2. Edit Code
  3. Find a theme.liquid file
  4. Add the following code at the bottom of the file above tag

If I managed to help you then, don’t forget to Like it and Mark it as Solution!

Best Regards,
Rock Technolabs

I followed this exact steps , it is still opening in new tab .

please send me code editor screenshots where you added this code

Found solution .

Thank You !!!

Good @Anonymous it solved using my code or do you use a different code?

I actually edited the code I initially used to open a new tab.