XHTML Strict Anchor Target Fix with jQuery
Since switching from HTML to XHTML, one of the things that kills my HTML Validator most is when I get lazy and put a target="_new" or target="_blank" in an anchor tag. So then I have to go back and fix them and find the old sitepoint article that describes how to fix the problem with a little javascript snippet. I was about about to do than when the thought occurred to me, why not just use jquery? I’m including the jquery library on every page anyway. It turns out to be rather simple.
I replace any target="_blank" with class="external", so my link would look something like this:
<a class="external" href="http://inzolo.com">Inzolo</a>
Then I write a little jquery snippet like so:
$(document).ready(function(){
$(‘a.external’).attr(‘target’, ‘_blank’);
});
That’s it!
Related posts:
- Simple PHP Pluralize I often find myself writing a if statement when I...
- Inconsistent Bugs and Hard Coded Variables Part of the joys of being a one man development...
Related posts brought to you by Yet Another Related Posts Plugin.
June 5, 2009
Tags: jquery, xhtml Posted in: Programming & Internet




6 Responses
Thank for this quick tips of yours. I am really having problem with this also. and you have my solution in problem. Well done and thank you so much.
I tend to believe that it should be left to user whether they want to open the link in new window or same window. Unless UI specifically asks for a new window, I will let the user choose. Both jquery and prototype can be used for ensuring some links are opened in new windows.
Well then I guess you tend to believe that most users know what they are doing… I’ve witnessed that they don’t. I don’t mind saying that because the users who don’t know what they are doing would never come across this blog.
Hi
this can be done with normal javascript. But the Jquery made it very simple and easly understandable
What is it with people who insist we should *always* let the user decide whether to pop up a new window or not?!? There are PLENTY of cases where we want to provide links for a user but we do not want them to leave the current page. (And where leaving the page would, in fact, represent an error condition.) Consider, for instance, an order checkout process that contains a link to the BBB, or a bank balance transfer process that allows you to “click for details” on a specific account. Etc, etc.
Thank you VERY much for this solution as the Javascript doesn’t seem to work any more and I’m certainly not going to remember
Go to www.!!!.com
Putting the JQuery library on your server is the “hard” part–then you get to “set it and forget it” by pasting in the scripts inside your elements.
I am perfectly cable of deciding which of my links should open into new windows, and I resent being expected to “settle” for XHTML Transitional simply because I wish to continue to!
Leave a Reply