Skip to content

Instantly share code, notes, and snippets.

@itzrahulsoni
Last active November 20, 2016 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itzrahulsoni/3f3a06714d725720c2495543b3864264 to your computer and use it in GitHub Desktop.
Save itzrahulsoni/3f3a06714d725720c2495543b3864264 to your computer and use it in GitHub Desktop.
Code Injection for Ghost so that the external links open in new tab.
<script>
// By default, Ghost blogging platform opens external links in the existing tab.
// You may the change the behavior if it is not desired.
// The following code opens all external links in a new tab.
$(document).ready(function() {
// Create a regex with current location
var regex = '/' + window.location.host + '/';
regex = regex.replace(/\./g,'\\\.');
var exp = new RegExp(regex);
// Find all links
$('a[href^=http]').each(function() {
if(!exp.test(this.href)) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
});
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment