Avoid using the target attribute and keep your site accessible.
Today I was asked to add the target attribute to a site we are developing. While compiling my argument.. I suddenly responded.. “It’s not valid code!”
While in many environments this may have caused tension; where I work this was just followed by laughter. Bryce immediately responded using BaseCamp to state: “Mike says it isn’t valid” which, in all fairness, is 100% accurate.
Like many of my colleagues, I always use the XHTML Strict DOCTYPE. There are numerous reasons for declaring a Strict DOCTYPE, but in order to keep my post short I won’t give you reasons why we choose to do so. I certainly am a snob about coding valid website’s from start to finish and will only say that if you are coding templates as a career please be a professional! Loose DOCTPYE’s are for learning or transitioning site’s only!
I wasn’t surprised at our response.. “Whatever!”, knowing that the leader of our pack loves his tabs and new windows. While my personal preference’s aren’t valid when discussing a client’s website, I feel that the control should be left to user, I don’t appreciate someone forcibly opening another tab without my permission, and I wouldn’t want to do that to anyone else. So without mentioning the chance that the user may be confused by us opening a new window and in the process, disabling their ability to use the back button, let’s consider accessibility!
There are many reasons to avoid using the target attribute, some may not be very important for your site please understand that some obscure browsers, screen readers, as well as mobile browsers do not support opening new windows. If you want to know all the reasons, Robert Nyman touches just about all the reasons between his post “How evil is the target attribute?” and the comments that follow. But for the reason of simply staying out of people’s way let’s see if we can find a better way.
I know there are always situations where you are either going to have to add them just by request or because you don’t make the end decision.. so let’s do it without switching DOCTYPE shall we?
$("a[rel*='external']").click(function(){
this.target = "_blank";
});
That’s it! If you are not familiar with jQuery or the attribute selectors, here’s what we did. I am telling jQuery to find all the anchor tags in the document that have an attribute “rel” containing the value “external”. Assign a click event to fire with the target attribute set to “_blank” which.. you guessed it.. opens the link in a new window or tab! So now all you do is inside your valid strict DOCTYPE you use:
Go read Mike's blog
Suddenly we are still adhering to the Strict DOCTYPE and not losing any face with our fellow coders and yet the client is tickled pink because even though you disagreed with her/his decision you apparently integrated the request.
Got a problem with what I wrote, agree or disagree? Let’s debate it!









