I want my external links to open in a new tab

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.

6 Likes