treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Open Link in a New Window

Last updated on:

HTML attribute (valid in HTML5 now):

<a href="http://chriscoyier.net" target="_blank">This link will open in new window/tab</a>

Inline JavaScript way:

<a href="http://chriscoyier.net" onclick="window.open(this.href); return false;" onkeypress="window.open(this.href); return false;">This link will open in new window/tab</a>

With jQuery:

See here

View Comments

Comments

  1. helen
    Permalink to comment#

    Target-blank is not strict xhtml (it might be considered as behaviour that should not be placed in the mark-up) and it can make people angry when used on every link, especially the internal ones. But sometimes it can be useful, if there’s an external link among many internal links.
    Personally I prefer to use CSS-Content to set a “(ext)” behind those links via auto-detection (attr).

  2. Ant

    I’d say it’s a bad practice. Because if I want to open link in new window, jsut press shift+mouse key. Or middle button (to open in new tab).

    • phoenix
      Permalink to comment#

      I’d say it’s the best practice to use target=”_blank”, because it simply works.
      There are people who don’t know that you can press the middle mouse button or other shortcuts. Nobody will see it at the end. Why should I not break the rule on this point?

  3. Thanks! PDFs don’t seem to honor _blank.

  4. This snippet was very useful, thank you!

  5. I think this article is misleading, it clearly states that target=”_blank” is invalid which isn’t true. It’s perfectly valid in both HTML5 and XHTML 1.0 Transitional. It’s only invalid in the strict DTD, but who uses that?
    There’s no real reason not to use the target attribute, every browser supports it and will support it for the foreseeable future. And besides, why use JavaScript for something that the browser can do natively?

Leave a Comment

Use markdown or basic HTML and be nice.