# Internal links in same tab, external links in new tab

**URL:** <https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048>\
**Category:** support\
**Created:** [March 14, 2018, 2:34pm UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048 "2018-03-14T14:34:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmansheim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bmansheim/32/5798_2.png) [@bmansheim](https://discourse.gohugo.io/u/bmansheim)\
**Post date:** [March 14, 2018, 2:34pm UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/1 "2018-03-14T14:34:55Z")

</div>

I want to have internal refs open in the same tab and external refs open in a new tab. I currently have this in my config.toml:

```auto
[blackfriday]
hrefTargetBlank = true

```

and a relref shortcode for internal links:

```auto
{{- if in (.Get 0) "/_index.md" -}}
	{{- $paths := (split (.Get 0) "_index.md") -}}
	{{- $pagepath := index $paths 0 -}}
	{{- $anchor := index $paths 1 -}}
	{{- with .Site.GetPage "section" (trim $pagepath "/") -}}
		{{- ( printf "%s%s" $pagepath $anchor ) | relLangURL -}}
	{{- end -}}
{{- else -}}
	{{- with .Site.GetPage "section" (.Get 0) }}
		{{- .URL -}}
	{{- else -}}
		{{- .Get 0 | relref .Page -}}
	{{- end -}}
{{- end -}}

```

---

<div class="post-metadata">

**Author:** ![lenymo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lenymo/32/4684_2.png) [@lenymo](https://discourse.gohugo.io/u/lenymo)\
**Post date:** [March 15, 2018, 11:30pm UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/2 "2018-03-15T23:30:50Z")

</div>

How is this working for you?

I personally prefer to handle this via vanilla JavaScript. If for whatever reason the JS doesn’t work there’s no great issue because links will still function. Perfect example of progressive enhancement.

I can share my code if you’re interested.

---

<div class="post-metadata">

**Author:** ![lucperkins](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lucperkins/32/1280_2.png) [@lucperkins](https://discourse.gohugo.io/u/lucperkins)\
**Post date:** [March 16, 2018, 12:14am UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/3 "2018-03-16T00:14:43Z")

</div>

@lenymo I would definitely be interested and I suspect I’m not alone in this forum 😀

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [March 16, 2018, 12:49am UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/4 "2018-03-16T00:49:58Z")

</div>

I don’t set that Blackfriday option, as I prefer just middle clicking on a link if I need to open it in a different tab.

But I am curious what you are trying to do in that shortcode. I didn’t understand your problem statement, and the solution you achieved with that shortcode.

---

<div class="post-metadata">

**Author:** ![lenymo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lenymo/32/4684_2.png) [@lenymo](https://discourse.gohugo.io/u/lenymo)\
**Post date:** [March 16, 2018, 2:32am UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/5 "2018-03-16T02:32:18Z")

</div>

Ok, here’s my vanilla JS function.

```
function handleExternalLinks() {

  // Variables.
  var links = document.links;
  var linksLength = links.length;
  var link;
  var linkHref;
  var hostname = window.location.hostname;

  // Loop through all links.
  for (var i = 0; i < linksLength; i++) {

    // Instantite the indivdual link.
    link = links[i];

    // Get the link's href attribute.
    linkHref = link.href;

    // If the link is to an external site or a javascript:void(0);.
    if (
      link.hostname != hostname &&
      linkHref !== 'javascript:void(0)' &&
      linkHref !== 'javascript:void(0);'
    ) {

      // Set the target of the link to _blank.
      link.target = '_blank';
    }
  }
} // handleExternalLinks()

```

As I said, I’m not overly fussed with the backward compatibility of my JS because as far as I’m concerned this is a value add not a core feature. A jQuery implementation of this would be more robust.

I would recommend running this in the footer of every page via handleExternalLinks().

A much simpler version of this code can be [found here](https://stackoverflow.com/questions/4425198/can-i-create-links-with-target-blank-in-markdown#answer-4425214).

---

<div class="post-metadata">

**Author:** ![lenymo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/lenymo/32/4684_2.png) [@lenymo](https://discourse.gohugo.io/u/lenymo)\
**Post date:** [March 16, 2018, 2:34am UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/6 "2018-03-16T02:34:57Z")

</div>

And please, if anyone spots any holes in my JS please pipe up.

---

<div class="post-metadata">

**Author:** ![bmansheim](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bmansheim/32/5798_2.png) [@bmansheim](https://discourse.gohugo.io/u/bmansheim)\
**Post date:** [March 21, 2018, 2:40pm UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/7 "2018-03-21T14:40:12Z")

</div>

@kaushalmodi My problem is that I want links to pages outside of my site to open in a new tab, but links to pages inside my site to open in the current tab.

The blackfriday option opens all links in a new tab, so I was thinking to either leave the blackfriday as is and put JS in my relref shortcode to open local links in the same tab.

I just don’t know how to do it or f it is even a good idea.

---

<div class="post-metadata">

**Author:** ![kaushalmodi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kaushalmodi/32/2567_2.png) [@kaushalmodi](https://discourse.gohugo.io/u/kaushalmodi)\
**Post date:** [March 21, 2018, 2:45pm UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/8 "2018-03-21T14:45:40Z")

</div>

> [@bmansheim](#):
>
> My problem is that I want links to pages outside of my site to open in a new tab, but links to pages inside my site to open in the current tab.

Hmm, based on the [`hrefTargetBlank` documentation](https://gohugo.io/getting-started/configuration/#blackfriday-options), I assumed that it already did that (_As I mentioned above, I leave that option at the default `false` as I anyways middle click on links to open them in separate tabs._) …

> [@](#):
>
> `hrefTargetBlank`  
> default: `false`  
> Blackfriday flag: `HTML_HREF_TARGET_BLANK`  
> Purpose: `true` opens external links in a new window or tab.

Does it not work that way?

If not, then maybe:

- Open an issue on Blackfriday repo

* * *

Looks like Blackfriday’s definition of “external link” is “not relative link”, which is bad IMO. At least in hugo docs, we should correctly document what `hrefTargetBlank` actually does… ~~I’ll open an issue for that in hugoDocs.~~ Done.

From Blackfriday repo:

- [condition to check when to insert `target="_blank"`](https://github.com/russross/blackfriday/blob/55d61fa8aa702f59229e6cff85793c22e580eaf5/html.go#L464-L467)
- [`isRelativeLink` code](https://github.com/russross/blackfriday/blob/55d61fa8aa702f59229e6cff85793c22e580eaf5/html.go#L904-L931)

* * *

> <https://github.com/gohugoio/hugoDocs/issues/428>
>
> Looks like.. unlike Blackfriday claims, \`hrefTargetBlank\` inserts \`target="\_blan…k"\` (not that I am a fan of \_blank targets) for \*\*all\*\* non-relative links i.e. absolute links.. even if the absolute links are internal, on the same domain.
> 
> So setting that to \`true\` doesn't necessarily mean that it will insert \_blank target \*only\* for external links.
> 
> See https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/8?u=kaushalmodi

---

<div class="post-metadata">

**Author:** ![inwardmovement](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/inwardmovement/32/5396_2.png) [@inwardmovement](https://discourse.gohugo.io/u/inwardmovement)\
**Post date:** [December 23, 2019, 3:17pm UTC](https://discourse.gohugo.io/t/internal-links-in-same-tab-external-links-in-new-tab/11048/9 "2019-12-23T15:17:58Z")

</div>

[https://discourse.gohugo.io/t/handle-external-links-with-render-hook-templates/](https://discourse.gohugo.io/t/handle-external-links-with-render-hook-templates/) may help you.
