Open Link in a New Window
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>
Valid code:
This link will open in new window/tab
I think some code might have gotten stripped there. Feel free to email me at chriscoyier@gmail.com to fix.
<a href="http://google.es" onclick="window.open(this.href); return false;">This link will open in new window/tab</a>
Hi,
Using the second approach opens the href in my current (parent) window too. i.e. my current window and new window both points to href value. I don’t want my parent window to be changed
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).
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).
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?
Thanks! PDFs don’t seem to honor _blank.
hey Jason, for PDF’s use, target=”_new”
This snippet was very useful, thank you!
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?