Skip to content

Instantly share code, notes, and snippets.

@seamusleahy
Last active December 16, 2015 05:59
Show Gist options
  • Save seamusleahy/5388747 to your computer and use it in GitHub Desktop.
Save seamusleahy/5388747 to your computer and use it in GitHub Desktop.
Open external links in a new window using jQuery.
// Open external links in new windows
jQuery(document).ready(function($) {
var re = new RegExp( '^([^/]+:)?//([^/]+\\.)?' + window.location.host.replace(/\./g, '\\.') + '/');
$('body').on('click', 'a', function(event) {
if(!re.test(this.href)) {
window.open(this.href, '_blank');
return false;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment