The
target attribute of the
<a> tag is deprecated in HTML 4.0 Strict and XHTML 1.0 Strict:
New-Window Links in a Standards-Compliant World
(Updated 2/12/2010 and 2/24/2010:)
I like the jQuery solution in
Justin Scheetz's comment on
this article in badlyDrawnToy:
$(function() {
$("a[href*='http://']:not([href*='"+location.hostname+"'])").click(
function() {
window.open(this.href);
return false;
});
});
(Updated again 2/24/2010:)
Here's the jQuery for just setting the target attribute (with a little help from
NerdyDork):
$(function() {
$("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target", "_blank");
});
(Updated again 10/06/2011:)
Found earlier that the above solution failed for https, so I had to add another line for that. And
then found that the comparison for .hostname needs to be case-insensitive. So my latest solution is the following, using some tips from the
electric toolbox (which also mentions that, with HTML5, "the W3C has un-deprecated the target attribute"):
$(function() {
$("a").filter(function() {
// find all links that have a hostname
// that doesn't match the current location
return this.hostname &&
this.hostname.toLowerCase() !== location.hostname.toLowerCase();
}).click(function() {
// open those external links on another window
window.open(this.href);
return false;
});
})