πŸ’‰ Code Injection Paste in: Footer

Open External Links in a New Tab

Make links to other websites open in a new tab on any Ghost site β€” readers keep your page open. One copy-paste code injection snippet, no theme edits.

#links #ux #javascript

Links you add in the Ghost editor always open in the same tab β€” there’s no setting to change it. This snippet makes every link that points to another website open in a new tab automatically, while links within your own site keep working normally. It works on every Ghost theme, because it looks at where the link goes, not at how your theme is built.

The Code

Paste this into Settings β†’ Code injection β†’ Site footer:

<script>
(function() {
document.querySelectorAll('a[href^="http"]').forEach(function(link) {
if (link.hostname && link.hostname !== window.location.hostname) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
}
});
})();
</script>

How to Install

  1. In Ghost Admin, go to Settings β†’ Code injection
  2. Paste the code into the Site footer section
  3. Click Save β€” done. Every outbound link on every page now opens in a new tab.

How It Works

The script finds every link on the page that starts with http, then compares the link’s domain with your site’s domain. If they differ, the link is external, and it gets target="_blank" (open in new tab) plus rel="noopener noreferrer" β€” the security attribute that stops the new page from being able to control the tab it came from.

Internal links β€” your posts, tags, pages β€” are untouched, so navigation on your own site stays in the same tab, which is what readers expect.

Customization

Exclude a specific domain (for example, keep links to your own shop in the same tab): add its hostname to the check:

if (link.hostname !== window.location.hostname &&
link.hostname !== 'shop.example.com') {

Only apply inside post content (leave navigation and footer links alone): replace the first line with

document.querySelectorAll('.gh-content a[href^="http"], article a[href^="http"]').forEach(function(link) {

Works With

Every Ghost theme β€” the snippet reads link destinations, not theme markup. All Luxe themes are compatible.