Code Snippets Gallery
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>
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>
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).