target=”_blank” XHTML replacement.

December 16th 2009

As you must already know, using target=”_blank” to open a page in a new window doesn’t validate in XHTML.

There’s a javascript workaround tho. This example is based on jQuery since it’s always included in my project and it’s my library of choice but it could be easily converted to any library.

The basics

To replace the target=”_blank” you should use the rel attribute. This attribute is used to specify the relationship  between the document that contains the link and the document it refers to. So your links should have the attribute rel=”external” to specify the link is not a link inside your website but to another website. There are plenty of values possible for you rel attributes, I invite you to read about microformats.

Now rel=”external” doesn’t mean target=”_blank”. Adding that attribute won’t open the links in new windows, it’s only a more semantic way of specifying the relationship between pages. Since some of you might not want to open rel=”external” in new windows, browser vendors didn’t implement it as being the same as a target=”_blank”. That when javascript comes into play.

The javascript


Easy isn’t it?

You actually need to either put that in a script of his own or at the bottom of you webpages. $(function(){ }); is actually the short document.ready call in jQuery. Once the document is ready, it’ll traverse through each link and add the attribute target=”_blank” to them. This way the W3C validator doesn’t see them and you page still validates.

Working markup

Here’s a working example with jQuery loaded.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
		<script src="" type="text/javascript"></script>
		<script type="text/javascript">
			google.load("jquery", "1.3.2");

		<script type="text/javascript" charset="utf-8">
		<a href="" rel="external">Google!</a>
		<a href="" rel="external">My site!</a>

Your opinion?

What do you think about this technique? I personally stopped caring about my pages not validating when I use target=”_blank”, but I know some people really want to see their page validates whatever the price.

65 Responses to “target=”_blank” XHTML replacement.”

  1. Michal says:

    It`s a workaround to pass the validation. I`m always against such practices.

    I use js to open new window and close it when needed. JS offers methods to do so and in my opinion there so no need to base on target deprecated attribute.

  2. JFFortier says:

    I’m not a big fan of opening pages in new windows. Most browsers allow me to control with a right click or a keyboard key where I want a link to open (either the same page, a new tab or a new window). Because most users don’t know about this feature, I feel obligated to code the opening of an external url in a new window so that they don’t quit my site. So using a technique like this is perfect; valid, semantic and clean. However, adding rel=”external” to all external links can be a lot of work and might not be possible using a cms wysiwyg. I use a similar technique:

    var allowedDomains = [location.hostname, “”];
    for ( var i=0; i -1 )
    return false;
    return true;
    .bind(“click”, function(){ return !; });

  3. Stephane says:

    @JFFortier: That’s a very clever technique. Never thought of it.

    The only thing, I always fear using thinking browsers might trigger this as being an unwanted popup, but never got around testing it everywhere. I guess you never had problem with it?

  4. JFFortier says:

    I never had any problems with pop-up blockers but I must admit I never did an extensive testing on all of them. However we can easily replace the bind() by .attr(‘target’,’_blank’). Actually, I’ll do that for now on.

  5. […] No margin for errors Te gustó este post? Compartelo en tus redes sociales […]

  6. archatas says:

    I would rather use instead of .attr(‘target’,’_blank’). I like the idea to use rel=”external”, but sometimes it might be useful to use class=”external” to show an icon next to the link.

    BTW, popup blockers block those popups which are opened by other events than click.

  7. essjayhch says:

    You don’t need an element class these days to match HTML elements, modern CSS selects things in exactly the same way that jQuery does – in other words as far as jQuery or CSS is concerned `.classname’ is semantically the same as `[class=”classname”]’.

    The effect of this is that you can pull out external anchors by doing a query of:

    a[rel=external] { /* Do some CSS styling */ }

  8. Your writing form is really good, you have a unusual style.

  9. I really like what you guys tend to be up too. This type of clever work and exposure!
    Keep up the superb works guys I’ve you guys to our blogroll.

  10. DIRK says:

    This is a pretty shady workaround/hack, but guess what? XHTML is an even bigger hack. You want your pages to validate? Use html, the non-braindead standard.

  11. whoah this blog iѕ fantastic і really like reading your articles.
    Stay սp the ǥood work! Yߋu realize, lots of peoole are lookjing гound for this info, ʏou can aid tɦem greɑtly.

  12. fixodent says:

    bookmarked!!, I like уour site!

  13. You really make it seem so easy with your presentation but I in finding this
    matter to be really something which I feel I’d by no means understand.

    It sort of feels too complex and extremely wide
    for me. I am having a look forward to your next publish, I will try to get the
    cling of it!

  14. I’m curious to find out what blog platform you’re using? I’m having some minor security issues with my latest website and I would like
    to find something more secure. Do you have any recommendations?

  15. What’s up, I desire to subscribe for this web site to get hottest updates,
    thus where can i do it please help out.

  16. I think the admin of this web site is really working hard in favor of his web site, since here every data is quality based stuff.

  17. Thanks for yor marvelous posting! I truly enjoyed reading
    it, you can be a great author.I will be sure to bookmark your blog annd definitely will come back in the foreseeable future.
    I want to eencourage you continue your great work,
    have a nice morning!

  18. news says:

    Appreciation to my father who informed me regarding this website, this website
    is in fact awesome.

  19. CockSucker says:

    The services of finding your date have come to your doorstep.
    and you might be not having disagreements about issues.
    When you make a long term association with a few targeted
    venues, you can negotiate better price terms and also obtain credit from the venue owners.

  20. Have you ever thought about writing an ebook or guest authoring on other sites?

    I have a blog centered on the same information you discuss and
    would really like to have you share some stories/information. I know my subscribers would
    value your work. If you are even remotely interested, feel free to shoot me an e-mail.

  21. link dafabet says:

    If you want to improve your familiarity just keep visiting this web page and be updated with the hottest
    news update posted here.

  22. Traiteur Rabat Regal; Traiteur de ronome au Maroc

    This is my expert

  23. prostatitis says:

    I really enjoy looking through on this web site, it has good articles.

  24. After exploring a handful of the blog articles on your web site, I truly
    appreciate your way of writing a blog. I bookmarked it to my bookmark webpage list and
    will be checking back soon. Take a look at my web site too and let me know your opinion.

  25. Corps des Ingénieurs du RNI au Maroc – هيئة المهندسين التجمعيين


  26. Corps des Ingénieurs du RNI au Maroc – هيئة المهندسين التجمعيين


  27. of course like your web site however you have to test the spelling on several of your posts.
    Many of them are rife with spelling problems and I in finding it
    very troublesome to inform the truth on the
    other hand I’ll certainly come again again.

  28. Remarkable things here. I’m very glad to see your article.

    Thanks a lot and I’m looking ahead to contact you.
    Will you kindly drop me a e-mail?

  29. permainan judi togel online adalah permainan yang sangat di gemari oleh kebanyakan masyarakat di asia terutama di indonesia, karena dengan adanya permainan judi togel online mereka bisa mendapatkan keuntungan yang banyak dengan mudah tanpa harus membuang banyak waktu, untuk itu apabila anda bergabung juga pastinya banyak bonus yang akan anda dapatkan dalam bermain judi togel online.

  30. Permainan judi togel online sekarang ini sangatlah digemari oleh masyarakat indonesia. Permainan yang simpel namun sangat dalam ini mulai berkembang sudah lama sebenarnya, namun dengan adanya bantuan internet permainan ini dapat dimainkan secara online yang membuat pemain merasa nyaman. Tentunya bermain di agen togel online terpercaya akan sangat menguntungkan anda!

  31. bandarbola says:

    There are definitely many things to know about the topic of this blog.

  32. Pasti ada banyak hal yang perlu diketahui tentang topik penting ini.
    Saya sangat menyukai semua pembahasan yang anda sajikan untuk dipelajari lebih dalam tentang blog tersebut.

  33. I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates.

  34. I really like all the discussions that you present for deeper study this forum and very helpful.

    Always so interesting to visit your site. What a great info, thank you for sharing. this will help me so much in my learning

    Very interesting contents of the articles you create. Make me add inspiration for the future.

  35. I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates.

  36. togelmaster1 says:

    Sukabet situs agen daftar poker terpercaya di indonesia yang telah menjadi pilihan dari banyaknya pecinta judi poker di Indonesia. Sukabet sendiri selalu memberika keuntungan besar bagi para pemainnya melalui bonus – bonus yang ada, bahkan dari deposit pertama.
    untuk informasi lebih lanjut kalian dapat mengunjungi, link berikut ini

    poker bonus new member 20 ribu

  37. With havin so muchh written content do you ever run into anyy
    issues of plagorism or copyright infringement? My website has a llot of unique content I’ve either created myself or outsourced but it seems a lot of it is popping it up all
    ovger thhe web without my agreement. Do you know any
    methods to hlp reduce content from being ripped off?
    I’d definitely appreciate it.

  38. Asiabet188 adalah situs agen judi bola online yang melayani daftar sbobet dan taruhan bola terpercaya indonesia lainnya. Kami memberikan layanan untuk anda agar dapat bermain Judi Bola, dengan adanya kami sebagai Agen Judi bola, maka setiap keinginan anda seperti pembukaan ID, withdraw, deposit dalam permainan judi bola dapat anda akses dengan mudah 24 jam online

  39. adalah situs judi online yang sangat populer sekarang ini, situs tersebut juga sudah dikenal oleh banyak orang khusus nya bagi para pencinta judi online. Situs tersebut memiliki banyak jenis permainan judi dengan pasaran terlengkap, tidak hanya itu situs tersebut juga menawarkan anda bonus-bonus yang menarik dan lebih besar pasti nya di bandingkan dengan situs judi online lainnya.

  40. Hello, I think your site might be having browser compatibility issues.
    When I look at your blog in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    excellent blog!

  41. Sukapoker – Hadir dalam genggaman anda kapanpun anda mau. Mainkan judi poker online pilihan dan terbaik di bidangnya hanya di Agen resmi IDN Poker milik situs Rasakan pengalaman modern dalam bermain judi poker online, melalui smartphone anda masing-masing. Raih kemenangan anda sekarang juga, dan buktikan bahwa anda adalah pemain judi poker terbaik di situs kami. Daftarkan diri anda dengan modal minim. Hanya dengan 10.000 rupiah saja.

  42. Bagi anda para penggemar permainan judi togel online, Berikut ini adalah beberapa tips dan trick jitu dalam bermain judi togel untuk menang hingga jutaan rupiah setiap hari nya
    cara menang togel 4 angka

  43. Bagi anda para penggemar permainan judi togel online, Berikut ini adalah beberapa tips dan trick jitu dalam bermain judi togel untuk menang hingga jutaan rupiah setiap hari nya

  44. Situs Togel says:

    Bagi anda para penggemar permainan judi togel online, Berikut ini adalah beberapa tips dan trick jitu dalam bermain judi togel untuk menang hingga jutaan rupiah setiap hari nya
    Situs Togel

  45. Mainkan permainan judi online resmi di situs IDN Poker sukapoker. Lalu nikmati penambahan deposit kepada anda tanpa syarat sama sekali.
    Pakai depositnya untuk bermain judi poker online di situs sukapoker sekarang juga. Buktikan bahwa anda adalah raja poker sejati di situs

  46. unsurtotoIND says:

    Untuk anda yang mencari bonus dalam bermain togel online, silahkan anda bergabung bersama unsurtoto bandar togel dengan bonus terbesar hingga 66 %. Untuk informasi lebih lanjut silahkan kunjungi link ini

Leave a Reply