Skip to content

Instantly share code, notes, and snippets.

@ubermichael
Created August 23, 2013 20:53
Show Gist options
  • Save ubermichael/6323890 to your computer and use it in GitHub Desktop.
Save ubermichael/6323890 to your computer and use it in GitHub Desktop.
Open all external links in a new window/tab. Scan all the links in an HTML document, and add target="_blank" to any that go to a different domain.
$(function(){
var hostname = window.location.hostname.replace('www.', '');
$('a').each(function(){
var link_host = $(this).attr('hostname').replace('www.', '');
if(link_host !== hostname) {
$(this).attr('target', '_blank');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment