Scroll Next Scroll Back

[jQuery Snippet] Open Links to Other Sites in a New Window

For some reason the target attribute is hard for a lot of non-tech people to remember, so I wrote this simple jQuery pattern now I just use this code.

$('a[href^=http://]:not([href^=http://'+location.hostname+'])')
.each(function(){$(this)
.attr('target','_blank');});

This one line gem will find any link that starts with http:// and if the domain is not the same as the current site, "location.hostname", it sets the target attribute to "_blank".