Last active
April 2, 2022 15:07
-
-
Save hidrees/76f7c0b3500f8da72ca4 to your computer and use it in GitHub Desktop.
Ghost: Open links in new tab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//By default, Ghost opens links in the existing tab. Insert this in your Ghost JS file to get all your links to open in a new tab instead! | |
$('a').each(function() { | |
var a = new RegExp('/' + window.location.host + '/'); | |
if(!a.test(this.href)) { | |
$(this).click(function(event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
window.open(this.href, '_blank'); | |
}); | |
} | |
}); |
I like more this one:
<script>
$(document).ready(function() {
$("a[href^='http']").attr("target","_blank");
});
</script>
Added in the footer of the Code Injection menu in the admin.
As, most of internal link are starting with '/', and they should be opened in the same TAB but, external links starts with http... and it is great to open them in the new TAB
You could also check this.href.indexOf(window.location.host) == -1
if it is in internal or external link.
See this blog article for more information and a detailed instruction how to use the code in Ghost and another HTML workaround for every post or page.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What must I add to make all external links nofollow?