Open links in new tab with Ghost CMS

If you are using Ghost CMS, which is brutally awesome BTW, then you might have already realized that all links your are adding via the Ghost CMS editor are opened in the same tab as your article.

In order to change this, you need to change the target of your <a href...> tags to "_blank".

You can do this with a JavaScript script after the DOM is generated which changes the target of all <a> tags in your posts/articles. To do so, add the following code to your footer Code injection:

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

If you want this to be a little more sophisticated, e.g you want all external links to be opened in a new tab but all internal ones in the same, you can use the following code:

<script type='text/javascript'>
  $( document ).ready(function() {
    let links = $(".gh-content a");
    $.each (links, function(index, value) 
        {
        	if(!value.href.includes(window.location.hostname)){
                value.target= "_blank";
            }
    	}
    )
  });
</script>

That's it. Now all your links are opened in a new tab.

This is inspired by the Ghost CMS forum.