Replacing target=”_blank” for Strict XHTML using JQuery (redux)

Some time ago, I provided a simple JQuery solution for replacing target="_blank" when writing Strict XHTML. However, one commenter recently pointed out that my solution does not work if the rel attribute contains more than one value e.g. rel="external nofollow"

Well, JQuery to the rescue again. A very slight modification to the Attribute Filter fixes this issue. The code now reads:

$(function() {
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
});

Notice the asterisk. This matches attributes that contain a certain value, as explained in the JQuery docs.

However, this isn’t the full redux. Thanks to the power of JQuery, we can identify external links without the need for the rel="external" attribute at all.

Assuming that all internal links are relative e.g. href="/contact", we can simply search for all hyperlinks whose anchor contains a fully qualified url, or more precisely starts with http e.g. href="http://some.site.com".

The final code now looks like this:

$(function() {
	$('a[href^=http]').click( function() {
		window.open(this.href);
		return false;
	});
});

And that’s the power of JQuery

Tags: ,

10 Comments

Woody Gilk commented on March 3rd, 2009 at 7:55 pm

There is one potential pitfall here, and that is if you site generates complete URLs, or if you use a complete URL to switch to HTTPS/HTTP. My only other recommendation would be to use “*=://” instead, as that would capture non-http(s) links as well. However, that be _less_ correct than your method, depending on what other types of resources you link to (RSS, for example).

Alun commented on March 5th, 2009 at 2:37 pm

forgive my ignorance but what does rel external do? Am I right to assume that it signals that the link is to an external site but does that mean that the browser is supposed to respect the links and open in a new window. I’m guessing not. So if a customer wants those links to open in a new window this javascript will allow that. How do you cater for people without javascript? Or is this just something that you cannot implement for non js users if you adhere to the strict DTD?

badlyDrawnToy commented on March 5th, 2009 at 4:32 pm

@Alun, A few good questions there. Without javascript, or web browser preferences, Strict DTDs do not allow links to be opened in a new window, as this is seen as an event i.e. not part of the document structure.

rel=”external” doesn’t actually do anything. The rel attribute defines the ‘link type’. According to the W3C Spec:

‘User agents, search engines, etc. may interpret these link types in a variety of ways. For example, user agents may provide access to linked documents through a navigation bar.’

There is a list of recognised link types, such as stylesheet. ‘external’ is not a recognised link type.

http://www.w3.org/TR/REC-html40/types.html#type-links

I bet some of the new browsers will soon offer the facility to identify such links though.

[...] second solution I came across fixed some of the issues of the first by re‐writing the regex to account for [...]

zmpl commented on July 7th, 2009 at 3:11 am

Another possible attribute name is

xlink:show=”new”

from the XLink spec.

Justin Scheetz commented on August 21st, 2009 at 2:04 pm

This will search your entire page for external links and make them open in a new window. Try it out!

$(function() {
$(“a[href*='http://']:not([href*='"+location.hostname+"'])”).click( function() {
window.open(this.href);
return false;
});
});

Keith commented on April 16th, 2010 at 1:28 am

Man, that is sweet. Thanks!

Evan Carroll commented on May 6th, 2010 at 5:33 pm

This is my favorite solution to this problem, because it uses JQuery, and doesn’t assume the XHTML capable browser has implemented HTML4 “target” attribute on links.

Frank commented on June 15th, 2010 at 3:13 pm

Remember – if you happen to generate an external link or two dynamically after the page loads, the new link won’t “pop” into a new window – unless you bind the click event to the external links with live().

$(‘a[rel*=external]‘).live(‘click’, function(){
window.open($(this).attr(‘href’));
return false;
});

This ensures that any new elements are bound to the modified click event, too.

Andrew commented on July 5th, 2010 at 2:47 pm

I find this nice way to do it:

$(“a[rel*=external]“).attr( “target”, “_blank” );

It’s valid, since target is still an attribute in the DOM definition.

Leave a Reply

CAPTCHA Image

Colophon

badlyDrawnToy is the blog of Howie Weiner, a knackered, old web developer, based in Manchester.

Howie is Technical Director at JP74 and specialises in PHP (Kohana) and Java (Spring) web applications, Search Engine Optimisation, custom CMS and web development.

View Howie Weiner's profile on LinkedIn
View Howie's twitter feed