# How can I make links open in a new tab

**URL:** <https://forum.ghost.org/t/how-can-i-make-links-open-in-a-new-tab/39829>\
**Category:** Using Ghost\
**Created:** [July 13, 2023, 1:52pm UTC](https://forum.ghost.org/t/how-can-i-make-links-open-in-a-new-tab/39829 "2023-07-13T13:52:00Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Theodorico\_Carvalho](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.ghost.org/theodorico_carvalho/32/20707_2.png) [@Theodorico\_Carvalho](https://forum.ghost.org/u/Theodorico_Carvalho)\
**Post date:** [July 13, 2023, 3:29pm UTC](https://forum.ghost.org/t/how-can-i-make-links-open-in-a-new-tab/39829/4 "2023-07-13T15:29:34Z")

</div>

I’ve tried to put these three bing AI generated codes on the ghost header, I’ve also tried putting them in the ghost footer, saved, and nothing happened.

these were the codes:

- `<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>`  
\*` <script type=“text/javascript”> var links = document.getElementsByTagName(“a”); for (var i = 0; i < links.length; i++) { if (links[i].hostname != window.location.hostname) { links[i].target = ‘_blank’; } } </script>`
- `<script type=“text/javascript”> var links = document.querySelectorAll(‘a’); links.forEach((link) => { if (link.href.includes(‘?xgtab&’)) { link.target = ‘_blank’; } }); </script>`

Also, this code that I’ve seen in another forum question in this link: [I want my external links to open in a new tab - Developer help - Ghost Forum](https://forum.ghost.org/t/i-want-my-external-links-to-open-in-a-new-tab/9830)

Which was this

```auto
<script type='text/javascript'>

$( document ).ready(function() {

$(".post-content a").attr("target","moredetail");

});

```

Then I tried this one

```auto
<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>

```

```auto

```

Which works, but only for external links

But I need it to open both internal and external links on new tab  
how can I do it?  
my website is [Panther Blog (pantherprotocol.io)](https://blog.pantherprotocol.io/)

**Update:**  
Slowly, I’ve come up with a code that does it for me  
which is this one:

```auto
<script>
var links = document.querySelectorAll('a');
links.forEach((link) => {
  if (link.href.indexOf("javascript") == -1 && link.href != "https://blog.pantherprotocol.io/#/portal/signup") {
    // check if the link is not a javascript function and not the signup link
    link.addEventListener('click', (event) => {
      event.preventDefault();
      event.stopPropagation();
      window.open(link.href, '_blank');
    });
  }
});
</script>

```

and it works perfectly, but only with the previously loaded blog articles. When I scroll further into the page, it doesn’t work with the articles that weren’t previously loaded. How can I solve this?

---

_[View the full topic](https://forum.ghost.org/t/how-can-i-make-links-open-in-a-new-tab/39829)._
