Skip to content

Instantly share code, notes, and snippets.

@martinwheeler
Last active September 8, 2016 09:11
Show Gist options
  • Save martinwheeler/e00a8b0d3f9352bbc092b4802c527137 to your computer and use it in GitHub Desktop.
Save martinwheeler/e00a8b0d3f9352bbc092b4802c527137 to your computer and use it in GitHub Desktop.
Add target _blank to all URLs which are external
jQuery('a').each(function() {
var url = jQuery(this).attr('href');
var httpRegex = new RegExp('https?:\/\/', 'i');
if (url.toLowerCase().indexOf(window.location.hostname) == -1 && httpRegex.test(url)) {
jQuery(this).attr('target', '_blank');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment