Skip to content

Instantly share code, notes, and snippets.

@rbk
Created February 6, 2015 15:26
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 rbk/cdb21744958bbbeba38e to your computer and use it in GitHub Desktop.
Save rbk/cdb21744958bbbeba38e to your computer and use it in GitHub Desktop.
Make links to other domains open in a new tab( requires jQuery)
function offSiteLinks() {
var all_links = $('a');
all_links.each(function(){
var link = $(this).attr('href');
var tld = 'com';
if( link != null ){
if( link.match( /\.pdf{1}$/ ) || ! link.match( '/' + location.hostname + '/' ) && ! link.match( /#/ ) && ! link.match(/mailto/) ){
$(this).attr('target', '_blank');
}
if( link.match('/' + location.hostname.replace(tld, 'org') + '/') ) {
$(this).attr('target');
}
if( link.match(/newson6/) ){
$(this).removeAttr('target');
}
if( link.match(/tel/) ){
$(this).removeAttr('target');
}
}
}); // end pdfs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment