[Dev] Target "_blank" for links on Ghost

After some days playing with Ghost, we're excited about the possibilities that it offers to us. From my personal point of view I found the Markdown editor lovely. However it lacks a important functionality regarding links: targeting the links in order to avoid users to exit your page.

You might say that one solution would be to press Command or Control key while clicking on the link to open the website in another tab but not all users are used to that behaviour and it's far from ideal.

I've searched on the Internet, and I might have missed it, but I didn't find any good solution for this1 . Anyway I came with that solution in JavaScript in order to automatically put the desired targeting to your outgoing links (and keep your users in your page if it's a link to your hostname).

var links = document.querySelectorAll( '.post-content a' );  
for (var i = 0, length = links.length; i < length; i++) {  
  if (links[i].hostname != window.location.hostname) {
    links[i].target = '_blank';
  } 
}

You can add this code, inside the <script> tags in Settings -> Code Injectionfrom your Ghost Admin Panel to get it working. Also, if you have a better solution you can send it to me to Twitter @wildgames Btw, that same snippet is being used in this blog. Also keep in mind that you may want to check the query selector if you're using a different theme than the default one.