# Open button links in new tab

**URL:** <https://forum.ghost.org/t/open-button-links-in-new-tab/40639>\
**Category:** Using Ghost\
**Created:** [August 22, 2023, 12:56am UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639 "2023-08-22T00:56:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![horkrvkz](https://avatars.discourse-cdn.com/v4/letter/h/e5b9ba/32.png) [@horkrvkz](https://forum.ghost.org/u/horkrvkz)\
**Post date:** [August 22, 2023, 12:56am UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639/1 "2023-08-22T00:56:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Cathy\_Sarisky](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/cathy_sarisky/32/25750_2.png) [@Cathy\_Sarisky](https://forum.ghost.org/u/Cathy_Sarisky)\
**Post date:** [August 22, 2023, 2:58am UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639/2 "2023-08-22T02:58:37Z")

</div>

```auto
<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](http://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…

---

<div class="post-metadata">

**Author:** ![horkrvkz](https://avatars.discourse-cdn.com/v4/letter/h/e5b9ba/32.png) [@horkrvkz](https://forum.ghost.org/u/horkrvkz)\
**Post date:** [August 22, 2023, 3:24am UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639/3 "2023-08-22T03:24:00Z")

</div>

Hey that worked, Thank you so much!

---

<div class="post-metadata">

**Author:** ![pmghost](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/pmghost/32/30061_2.png) [@pmghost](https://forum.ghost.org/u/pmghost)\
**Post date:** [April 23, 2024, 11:03pm UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639/4 "2024-04-23T23:03:09Z")

</div>

This code works for Link and Button:

```auto
<script>
document.addEventListener('DOMContentLoaded', function() {
  // Target external links
  var extLinks = document.querySelectorAll('a[href*="://"]'); // Select links with external URLs
  for (var i = 0; i < extLinks.length; i++) {
    if (!extLinks[i].href.startsWith(window.location.origin)) { // Exclude busynesspro.com links
      extLinks[i].setAttribute('target', '_blank');
    }
  }

  // Target buttons (assuming they lead somewhere external)
  var buttons = document.querySelectorAll('button');
  for (var i = 0; i < buttons.length; i++) {
    if (!buttons[i].hasAttribute('href')) { // Only target buttons without href attribute (likely for internal navigation)
      buttons[i].setAttribute('target', '_blank');
    }
  }
});
</script>

```

---

<div class="post-metadata">

**Author:** ![Ismery\_Moreno](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ismery_moreno/32/31639_2.png) [@Ismery\_Moreno](https://forum.ghost.org/u/Ismery_Moreno)\
**Post date:** [September 4, 2024, 9:54pm UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639/5 "2024-09-04T21:54:41Z")

</div>

Hi pmghost,

Where can i include the code you shared, I added a button in my post using button content type, I am not available to include any code.  
How can I add ‘target’, ‘\_blank’ in this button?

see my post:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/ghost2/original/3X/c/2/c2b7d76fe3fa0280fc9c82b3b3702546963efab4.png)

---

<div class="post-metadata">

**Author:** ![Raki](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/raki/32/40808_2.png) [@Raki](https://forum.ghost.org/u/Raki)\
**Post date:** [September 4, 2024, 10:40pm UTC](https://forum.ghost.org/t/open-button-links-in-new-tab/40639/6 "2024-09-04T22:40:26Z")

</div>

Navigate to Ghost settings \> Code injection \> Site header, and put the code there.
