Code Snippet

Home » Code Snippets » HTML » Open Link in a New Window

Open Link in a New Window

Invalid, but works:

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

Valid, but obtrusive:

<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

Subscribe to The Thread

  1. Driews
  2. helen

    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).

  3. 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

      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?

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

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~