Last active
November 20, 2016 12:27
-
-
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.
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
<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