-
-
Save kirtan403/3fc78b71631895da550f1c3092a3c9f6 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
<script> | |
// By default, Ghost opens links in the existing tab. | |
// Insert this in your Ghost code injection footer | |
// to get all your links to open in a new tab instead! | |
$(document).ready(function() { | |
$('a[href^=http]').each(function() { | |
// Get the current host and replace '.' with '\.' | |
var regex = '/' + window.location.host + '/'; | |
regex = regex.replace(/\./g,'\\\.'); | |
// Create a regular expression to check with link's href | |
var regexp = new RegExp(regex); | |
// If link is not from the current host add a jQuery on click function | |
// to open the link in a new window/tab | |
if(!regexp.test(this.href)) { | |
$(this).click(function(event) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
window.open(this.href, '_blank'); // Open in a new window | |
}); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment