Open button links in new tab

I can’t figure out how to make an external link within a button open in a new tab, and I don’t see anyone else asking about this either. Anyone willing to help?

<script>
const externalLinks = document.querySelectorAll('a[href^="http"]:not([href^="https://demosite.com"])');
externalLinks.forEach(link => {
  link.target = '_blank';
});
<script>

The code above will open all links that don’t go to demosite.com in a new link. (Update for your site, obviously.)

To target a single button, you’re going to need to figure out how to select it. Most ghost buttons are actually ‘a’ tags, so making that selector a.kg-header-card-button for example would let you select for just header card buttons. Or a.kg-btn for buttons in the body of a post… or…

3 Likes

Hey that worked, Thank you so much!

1 Like