Copter Labs Copter Labs

Smart Design.

For Smart People.

Hold on... This isn't EnnuiDesign.com — What Gives?

It's been a long time coming, but Jason Lengstorf, formerly of Ennui Design, has expanded his team to include Drew Douglass, Rob MacKay, Henry Moran, and Tom Sturge.

It didn't feel right to keep the same name, so we decided to continue on as Copter Labs. You can expect the same great content under this new name!

Easily Create External Links Without the Target Attribute

Today's tip is extremely short and simple, and to a lot of folks may be a "Duh!" sort of tip, but I felt it was worth sharing.

I'm a big fan of keeping sites valid in XHTML 1.0 Strict. When I first started paying attention to standards, one of the things that stumped me right off the bat was the use of external links.

The Problem with target="_blank"

As I'm sure most people know, the use of the target attribute isn't considered valid. However, in order to open links in a new page, the only tool provided by HTML is the target attribute!

I had been creating external links for as long as I could remember using the following format:

<a href="http://example.com" target="_blank">External link</a>

The Fix: rel="external"

The HTML

When I started using jQuery, I stumbled across this little gem that allowed me to have my valid code and external links with one simple line of code:

<a href="http://example.com" rel="external">External link</a>

The use of rel="external" is perfectly valid, and doesn't cause any problems at all. However, this attribute won't work by itself. In order to cause the links to open in a new page, we first need to employ a jQuery one-liner.

The JavaScript

At the bottom of any page that features external links, we need to include one line of jQuery code (and jQuery itself, of course):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
	$('a[rel="external"]').attr('target', '_blank');
</script>

And that's it. Seriously.

This method degrades gracefully: if the user doesn't have JavaScript, the link will open in the same window. This method is easy, lightweight, and most importantly, valid XHTML 1.0 Strict.

Summary

Writing valid code should be high on any developer's to-do list, and using the rel="external" trick is an easy way to take yourself one step closer to completely valid sites.

Do you have any tricks to keep sites valid? How about a raw JavaScript, MooTools, or other library method of doing the rel="external" trick? Share it in the comments!

Date. 06/16/2009

Comments. 26

Category. jQuery

Jason Lengstorf

Jason Lengstorf

Jason Lengstorf a turbogeek hailing from Portland, Oregon. He designs and develops websites using PHP, MySQL, JavaScript (jQuery), CSS, and HTML. He's written two books (PHP for Absolute Beginners [2009 Apress] and Pro PHP and jQuery [2010 Apress]), and he's written articles on development and design for Nettuts, CSS Tricks, and Smashing Magazine, among others.

Was This Post Helpful? Pass It On!

Share the Love

If this post taught you something, reminded you of something you had forgotten, or just made you feel good, there's really no better way to say "thank you" than passing it along to your friends.

Don't forget to like us on Facebook, join our newsletter, and/or subscribe to our RSS feed to make sure you hear about new posts first!

Join Our Gaggle of Geeks
* indicates required

Comments.

  1. Gravatar

    Nice tip (and by the way thanks for all the usefull articles ! ) but (yeah sorry... ) isn't that just a way to go around w3c rules ? Because what w3c tried to avoid is not the use of target _blank but the fact that the page opens in a new window. So the validator is going to validate your code but this method is still not 100% strict valid ;)

    But anyway, thanks for the tip. It can still be used for other problems :)

  2. Gravatar

    @Simon:

    Yes, it's a workaround.

    The use of new windows is a grey area in that clients demand it even though it's frowned upon.

    On personal projects and client projects that don't specifically request external links, I avoid them. However, when it can't be avoided, I'd rather use a workaround and still have sites validate.

    Thanks for your input!

  3. Gravatar

    Ok, my bad. I thought you were talking about personal projects ;)

    And I guess you're right to point this out for some client projects.

  4. Gravatar

    Simple idea for a ITT:

    Tutorial for beginners:

    Create a: "you have to enable javascript to view this page"-message

  5. Gravatar

    @Rodney That is actually a really easy request. Simply use the noscript tag.

    You have to enable javascriprt to view this page

  6. Gravatar

    The greatest gifts in life are not purchased, but acquired through hard work and determination. Find the star that twinkles in your heart?for you alone are capable of making your brightest dreams come true. Give your hopes everything you've got and you will catch the star that holds your destiny.

  7. Gravatar

    Take pride in your accomplishments, as they are stepping stones to your dreams. Understand that you may make mistakes, but don't let them discourage you. Value your capabilities and talents for they are what make you truly unique.

  8. Gravatar

    As I'm sure most people know, the use of the target attribute isn't considered valid. However, in order to open links in a new page, the only tool provided by HTML is the target attribute

    !

  9. Gravatar

    Legend of Zhong Qing court the remaining two people: 1: no money man 2: The woman is not packaged

  10. Gravatar

    abercrombie and fitch uk sale

  11. Gravatar

    moncler jacken online shop

  12. Gravatar

    abercrombie and fitch uk sale

  13. Gravatar

    Patience is most bitter, yet most sweet the fruit it birth

  14. Gravatar

    This is my first time i visit here . I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.

  15. Gravatar

    I found so dozens entertaining fabric in your blog, especially its discussion. From the tons of comments on your articles, I estimate I am not the only one owning all the kicks here! Keep up the good work.

  16. Gravatar

    Nice collections!!! great source….Thanks for the post! ??

  17. Gravatar

    This is my first time comment at your blog.Good recommended website.

  18. Gravatar

    I found so loads entertaining fabric in your blog, especially its discussion. From the tons of comments on your articles, I estimate I am not the only one owning all the enjoyment here! Keep up the good work.

  19. Gravatar

    I also want to learn, who can teach me....

  20. Gravatar

    Louboutin Shoes,Christian Louboutin Shoes,Louboutin Shoes Outlet,Louboutin Shoes 2012,Christian Louboutin Outlet

    "the article is composed by seoersong 20120417"

    http://www.louboutinshoesoutlet2012.com/

  21. Gravatar

    Great news update!! I am wondering how you guys manage to find such kind of information so early.

  22. Gravatar

    Good!!!! What you said is right

  23. Gravatar

    Thanks for a nice share you have given to us with such an large collection of information. travelling tips

  24. Gravatar

    Thanks, you have made it easy for me to understand.

  25. Gravatar

    I am currently working on an assignment and I have been exploring your blog for a few hours. Thank you for your post it proved helpful for me.

  26. Gravatar

    Good Post. It is really very nice. Thank you!

Join In.

Have something to say? By all means, speak up!

But first, a few rules:

  • Don’t be a jerk.
  • Use your real name, not your business name - this is a discussion, not a billboard.
  • Only <strong>, <em>, and <code> are allowed tags.
  • Wrap code samples in <code> tags.

Happy commenting!

Add a Comment