# I want my external links to open in a new tab

**URL:** <https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830>\
**Category:** Developer help\
**Created:** [October 28, 2019, 8:33pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830 "2019-10-28T20:33:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gianni](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@gianni](https://forum.ghost.org/u/gianni)\
**Post date:** [October 28, 2019, 8:33pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/1 "2019-10-28T20:33:37Z")

</div>

Hello all

I’d like all external links on my blog to open in a new tab.  
I’ve been reading the blog posts on this forum from people that had a similar question. I found some possible solutions that were posted here, but none of them seem to work on my Ghost website. The current version of Markdown seems to overrule attempts to manually create links like  
[Text goes here]([http://linkgoeshere](http://linkgoeshere)" target=“\_blank)  
and take out the target=”\_blank" part.

Does anyone know if there is a way to overrule this, without using Javascript?

PS: I noticed that some of the posts around this topic were closed by admins, stating that links should never open in a new tab, using some old blog posts around the topic dating as far back as 1999 to prove their point.

Please, do not close this blog post with the same kind of response. I’d like to get an actual workaround from the community.

I personally don’t care how links in HTML and Markdown were initially intended to work and that they “should” never open in a new tab. I don’t agree with that stance because it is a technology push without taking the user experience into account, which is shortsighted and obnoxious. Most users and a lot of webmasters expect external links to open in a new tab. Period.

By the way, here’s a good article about why external links should open in a new tab: [Why External Links Should Open in New Tabs](https://uxmovement.com/navigation/why-external-links-should-open-in-new-tabs/)

---

<div class="post-metadata">

**Author:** ![jeff](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/jeff/32/4900_2.png) [@jeff](https://forum.ghost.org/u/jeff)\
**Post date:** [October 28, 2019, 9:28pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/2 "2019-10-28T21:28:40Z")

</div>

but it’s so easy in JS… and instead of **\_blank** I just use a single named tab (e.g. _moredetail_ ) so that I don’t spam my users by opening lots of tabs.

I have the following in my **Code injection** Site Footer:

```
<script type='text/javascript'>
  $( document ).ready(function() {
  	$(".post-content a").attr("target","moredetail");
  });
</script>
```

---

<div class="post-metadata">

**Author:** ![jeff](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/jeff/32/4900_2.png) [@jeff](https://forum.ghost.org/u/jeff)\
**Post date:** [October 28, 2019, 9:51pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/3 "2019-10-28T21:51:11Z")

</div>

If you only wanted to do this for specific links, you could simply embed a tag in the href and look for the tag.

For example; say I decide to use: **?xgtab&** as an indicator to open the page in an [External Giani Tab]

- open in a new tab: [The domain name newtab.com is available for rent](https://www.newtab.com?xgtab&other=arg&goes=here&etc=etc)
- open in current tab: [On Site Machining Worldwide | In-Place Machining Company](https://www.inplace.com?other=arg&goes=here&etc=etc)
- open in a new tab: [https://www.elsewhere.com?xgtab&](https://www.elsewhere.com?xgtab&)

and then just use a more advanced selector:

```
$( "a[href*='?xgtab&']" ).attr("target","moredetail");

```

This will embed the _xgtab_ arg in the request - but I doubt that will cause a problem if you choose a tag that is unlikely to be used by the websites you are linking to.

---

<div class="post-metadata">

**Author:** ![gianni](https://avatars.discourse-cdn.com/v4/letter/g/a4c791/32.png) [@gianni](https://forum.ghost.org/u/gianni)\
**Post date:** [October 29, 2019, 12:03pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/4 "2019-10-29T12:03:00Z")

</div>

Aha, this is actually very usefull. Just what I need. Thanks!

---

<div class="post-metadata">

**Author:** ![pattersonmatilda062](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@pattersonmatilda062](https://forum.ghost.org/u/pattersonmatilda062)\
**Post date:** [November 26, 2019, 10:31am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/5 "2019-11-26T10:31:45Z")

</div>

I am a photographer and I have a [Facebook](https://www.facebook.com/) page. I want an internal link on my Facebook page to my business website to get more traffic. But I don’t know how to do this.

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [November 26, 2019, 10:38am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/6 "2019-11-26T10:38:42Z")

</div>

Is this question related to a Ghost site? 🙂

---

<div class="post-metadata">

**Author:** ![denvergeeks](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/denvergeeks/32/7401_2.png) [@denvergeeks](https://forum.ghost.org/u/denvergeeks)\
**Post date:** [December 8, 2019, 4:56am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/7 "2019-12-08T04:56:58Z")

</div>

@DavidDarnes the reply above by @pattersonmatilda062 looks like a spammer message to me – not related to Ghost site as far as I can tell.

---

<div class="post-metadata">

**Author:** ![Sumeet\_M](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/sumeet_m/32/6183_2.png) [@Sumeet\_M](https://forum.ghost.org/u/Sumeet_M)\
**Post date:** [February 5, 2020, 10:14am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/8 "2020-02-05T10:14:01Z")

</div>

Hi Jeff,

I added the code in the side footer but its not working for me. All the links are still opening in the same tab.

Do I need to do anything apart from adding the code in the site footer?

Thank you. 🙂

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [February 5, 2020, 3:02pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/9 "2020-02-05T15:02:56Z")

</div>

I think @jeff’s version expects jQuery to be present, here’s a vanilla JavaScript version:

```js
[...document.querySelectorAll("a[href*='?xgtab&']")].forEach(link => {
	link.setAttribute("target", "moredetail");
});

```

> [@I want my external links to open in a new tab](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/3):
>
> If you only wanted to do this for specific links, you could simply embed a tag in the href and look for the tag. For example; say I decide to use: ?xgtab& as an indicator to open the page in an [External Giani Tab] open in a new tab: [The domain name newtab.com is available for rent](https://www.newtab.com?xgtab&other=arg&goes=here&etc=etc) open in current tab: [On Site Machining Worldwide | In-Place Machining Company](https://www.inplace.com?other=arg&goes=here&etc=etc) open in a new tab: [https://www.elsewhere.com?xgtab&](https://www.elsewhere.com?xgtab&) and then just use a more advanced selector: $( "a[href\*='?xgtab&']" ).attr("tar…

---

<div class="post-metadata">

**Author:** ![denvergeeks](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/denvergeeks/32/7401_2.png) [@denvergeeks](https://forum.ghost.org/u/denvergeeks)\
**Post date:** [February 15, 2020, 5:33pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/10 "2020-02-15T17:33:00Z")

</div>

Welcome to Ghost community, @Sumeet_M !

Here are several ways I do this on my sites:

[https://ghost-o-matic.com/make-links-open-in-a-new-tab/](https://ghost-o-matic.com/make-links-open-in-a-new-tab/)

---

<div class="post-metadata">

**Author:** ![ajeet](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ajeet/32/6345_2.png) [@ajeet](https://forum.ghost.org/u/ajeet)\
**Post date:** [February 15, 2020, 6:37pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/11 "2020-02-15T18:37:58Z")

</div>

Just a newbie question: - Won’t this jQuery make the specific page slow ?

Since I was missing this feature, I posted a similar question (labelled as IDEA) in the forum a day ago. [Link](https://forum.ghost.org/t/add-link-target-or-rel-attribut-from-post-editor/11992)

---

<div class="post-metadata">

**Author:** ![DavidDarnes](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/daviddarnes/32/3533_2.png) [@DavidDarnes](https://forum.ghost.org/u/DavidDarnes)\
**Post date:** [February 17, 2020, 9:06am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/12 "2020-02-17T09:06:36Z")

</div>

Hey @ajeet, the JavaScript I wrote doesn’t need jQuery and will apply the attribute instantaneously:

> [@DavidDarnes](#):
>
> ```auto
> [...document.querySelectorAll("a[href*='?xgtab&']")].forEach(link => {
> link.setAttribute("target", "moredetail");
> });
> 
> ```

---

<div class="post-metadata">

**Author:** ![ajeet](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/ajeet/32/6345_2.png) [@ajeet](https://forum.ghost.org/u/ajeet)\
**Post date:** [February 17, 2020, 9:10am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/13 "2020-02-17T09:10:53Z")

</div>

Thank you David. You are kind 🙂

---

<div class="post-metadata">

**Author:** ![GTS](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/gts/32/6913_2.png) [@GTS](https://forum.ghost.org/u/GTS)\
**Post date:** [March 20, 2020, 5:17pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/14 "2020-03-20T17:17:14Z")

</div>

Hi thanks for this code, so we only neeed to inject this in the footer? Not working for me. IS there something I’m missing?

---

<div class="post-metadata">

**Author:** ![mikeywil](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/mikeywil/32/6880_2.png) [@mikeywil](https://forum.ghost.org/u/mikeywil)\
**Post date:** [March 21, 2020, 5:26pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/15 "2020-03-21T17:26:09Z")

</div>

Hi @GTS, I think you’d also need to add attributes to the link ( **?xgtab&** is the example) as per @jeff’s earlier post - see [I want my external links to open in a new tab - #3 by jeff](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/3)

---

<div class="post-metadata">

**Author:** ![Aileen](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/aileen/32/9846_2.png) [@Aileen](https://forum.ghost.org/u/Aileen)\
**Post date:** [April 27, 2020, 4:54am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/16 "2020-04-27T04:54:08Z")

</div>

I was successfully using this non-jQuery code snippet injected in the Ghost site footer:

```
<script type="text/javascript">
    var links = document.querySelectorAll('a');
    links.forEach((link) => {
        var a = new RegExp('/' + window.location.host + '/');
        if(!a.test(link.href)) {
          	link.addEventListener('click', (event) => {
                event.preventDefault();
                event.stopPropagation();
                window.open(link.href, '_blank');
            });
        }
    });
</script>

```

This doesn’t name the tab, but checks if the URL is external and only then opens in a new tab.

---

<div class="post-metadata">

**Author:** ![gr36](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/gr36/32/6491_2.png) [@gr36](https://forum.ghost.org/u/gr36)\
**Post date:** [April 27, 2020, 3:01pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/17 "2020-04-27T15:01:29Z")

</div>

Unsuccessfully tried all variations of these snippets to little or no effect.

This really should be an option in the settings, and things such as “but it’s so easy in JS…” is only true if you know JS, or you are fine with adding more scripting into your website.

Ghost needs to become less about writing scripts and more about writing, and options such as this will help remove the barrier to entry. I manage to stumble my way through most things, but this has been bugging me for a while.

This [code finally worked](https://www.insidersbyte.com/open-external-links-in-a-new-tab-by-default-ghost/) for me

```
<script>
  $(document).ready(function() { 
	$("a[href^='http']").attr("target","_blank");
  });
</script>

```

No ideas why others didn’t. I am eternally grateful to the post in this forum though… they usually save me some pain!

---

<div class="post-metadata">

**Author:** ![Aileen](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/aileen/32/9846_2.png) [@Aileen](https://forum.ghost.org/u/Aileen)\
**Post date:** [April 28, 2020, 1:38am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/18 "2020-04-28T01:38:25Z")

</div>

I had a typo in my code snippet. If you try it again, it should work and only open external links in a new tab, but not internal ones.

Your snippet requires jQuery, which is not something everyone wants to use.

---

<div class="post-metadata">

**Author:** ![afragos](https://avatars.discourse-cdn.com/v4/letter/a/41988e/32.png) [@afragos](https://forum.ghost.org/u/afragos)\
**Post date:** [April 29, 2020, 5:49pm UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/19 "2020-04-29T17:49:47Z")

</div>

I know how to do it. But I find it difficult to explain it here. It is a lot of writing. It would be better if I could talk to you. And you will immediately try to make amendments. What time zone are you in? I am from Canada, [Canada Population (2023) - Population Stat](https://populationstat.com/canada/), but I am available to talk round the clock. Call me if you do not find any other solution. I will help with pleasure.

---

<div class="post-metadata">

**Author:** ![giacomosilli](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/giacomosilli/32/3403_2.png) [@giacomosilli](https://forum.ghost.org/u/giacomosilli)\
**Post date:** [May 13, 2020, 9:54am UTC](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830/20 "2020-05-13T09:54:52Z")

</div>

> [@Aileen](#):
>
> I was successfully using this non-jQuery code snippet injected in the Ghost site footer:
> 
> ```auto
> <script type="text/javascript">
> var links = document.querySelectorAll('a');
> links.forEach((link) => {
> var a = new RegExp('/' + window.location.host + '/');
> if(!a.test(link.href)) {
> link.addEventListener('click', (event) => {
> event.preventDefault();
> event.stopPropagation();
> window.open(link.href, '_blank');
> });
> }
> });
> </script>
> 
> ```
> 
> This doesn’t name the tab, but checks if the URL is external and only then opens in a new tab.

This is excellent, but I just noticed that clicking on the avatar image at the top right, opens another tab instead of opening the dropdown menu.

It also happens by clicking on the choice of the stripe plan, it opens a new tab.

Any ideas to fix it?

**EDIT**  
this seems to work, if it can be optimized, I would like to know

```
 <script>
	var links = document.querySelectorAll('a');
	links.forEach((link) => {
		var a = new RegExp('/' + window.location.host + '/');
		if(link.href.indexOf("javascript") == -1){
			if(!a.test(link.href)) {
				link.addEventListener('click', (event) => {
					event.preventDefault();
					event.stopPropagation();
					window.open(link.href, '_blank');
				});
			}
		}
	});
</script>

```

[Next page](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830.md?page=2)
