Option for anchor to open a new window or stay on site

Hi there, @Paul_Hayes1 :wave:

Our devs came up with 2 solutions for your case.

The 1st code will open all links in the same tab:

const waitForElem = (selector) =>
  new Promise((resolve) => {
    if (document.querySelector(selector)) {
      return resolve(document.querySelector(selector));
    }

    const observer = new MutationObserver(() => {
      if (document.querySelector(selector)) {
        observer.disconnect();
        resolve(document.querySelector(selector));
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  });

waitForElem('#__EAAPS_PORTAL[class*="eapps-blog"]').then((popup) => {
  popup.addEventListener('click', (e) => {
    const link = e.target.closest('a');

    if (link?.href) {
      e.preventDefault();
      window.open(link.href, '_self');
    }
  });
});

And the 2nd code will open the links from the website where the widget is installed in the same tab. Links from other websites will be opened in a new tab:

const waitForElem = (selector) =>
  new Promise((resolve) => {
    if (document.querySelector(selector)) {
      return resolve(document.querySelector(selector));
    }

    const observer = new MutationObserver(() => {
      if (document.querySelector(selector)) {
        observer.disconnect();
        resolve(document.querySelector(selector));
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  });

waitForElem('#__EAAPS_PORTAL[class*="eapps-blog"]').then((popup) => {
  popup.addEventListener('click', (e) => {
    const link = e.target.closest('a');

    if (link && link.href.startsWith(window.location.origin)) {
      e.preventDefault();
      window.open(link.href, '_self');
    }
  });
});

Try it out and let us know if it worked for you :slightly_smiling_face:

We agree that it would be great to have this feature right in the settings and I’ve added this idea to the Wishlist - Setting to open links in new/same tab

1 Like