I want my external links to open in a new tab

Hi everyone, I’ve been trying to follow along with the ideas above. But I’m still having some trouble. I’m mainly wondering about links within blog posts. I’ve been using bookmarks and external links… with this script work for those too?

Thanks for any help

1 Like

They should work for bookmark links, where are you encountering problems? Can you share the code you’re using? :blush:

1 Like

Thanks for the reply, I forgot to come back and say I figured it out. I got that script from just above to work in the end :ok_hand:

1 Like

Impressive codes this is what i am looking for for the past 2years haha. thank you so much!

1 Like

How do you create a single named tab? So I can use this to open external links in new tabs. Help would be greatly appreciated

If you’re looking for a simple target="_blank" functionality, I’ve been using it for quite a while now.
What below codes does, is check the hostname you’re linking to, compare it to the hostname of your blog. If they don’t match it adds target="_blank" to that link. Links within your domain are left alone.

$(function() {
	// add target="_blank" to all non-internal text links
	$('.post-full-content a').filter(function() {
		return this.hostname && this.hostname !== location.hostname;
	}).attr('target', '_blank');

It’s a slightly different approach from the accepted answer, but I hope that helps someone.


Thank you, is this something that will work in the code injection part of my ghost site?

Thank you! This worked! I am not familiar with code but I just added the script tags this code and it is working now thanks!

1 Like

You set the TARGET attribute to a fixed name.

If you use _BLANK each link will open in a new blank tab.

If you set it to MYNEWTAB or some other value that you choose, the first link will open a new tab and subsequent links will use this named tab.

More details on how HTML links work can be found here. https://www.w3schools.com/jsref/prop_anchor_target.asp

Wow. Thank you Denver

Thank you so much for this.

Jeff, thank you!! Works like a charm.

I am new to Ghost and was wondering why the heck “open in new tab” wasn’t an option with external link. I completely agree with another user that is proper UX.

Thank you so much for this fix. I tested it on my site and worked well.

Thank you both for this - works well for me