External Links: Open In New Window

Hello, is there a solution/Code to set all external Links as ‘target=“_blank”’ . There are a view solutions to find via Google but they do not work with switchthemes. Any ideas on that? Thank you

Hey @arsvobiscum

Yes, in the file layout/theme.liquid, find the closing </head> tag and add this code snippet before it:

<script>
  document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('a[href]').forEach((linkEl) => {
      if (linkEl.hostname !== location.hostname) {
        linkEl.target = '_blank';
      }
    });
  });
</script>

Let me know if this works!

Thank you. That worked!