28 Apr, 2009
Open link in a new window using jQuery
HTML:
<a href="http://www.google.com" rel="external">Google</a>
Javascript:
$(document).ready(function() {
$("a[rel=external]").attr('target', '_blank');
});
Or
$(document).ready(function() {
$('a[rel="external"]').click( function() {
window.open( $(this).attr('href') );
return false;
});
});
Love what you've just read? Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
Other similar posts that you might be interested in:
- 10 Useful jQuery Snippets
- New-Window Links in a Standards-Compliant World
- Click to copy and open site script
- Back to top link with jQuery
- How to ‘Post on Facebook Wall’ on iPhone and Android using PhoneGap plugins
- Troubleshooting jQuery.get() Cache issue in Internet Explorer
- Code to embed Google Map and Street View
Hello! Welcome to Web development blog! My name is Ei Sabai and on this blog, I write about web development, mobile app development, latest web technologies and the likes. Read more 


examples please, i can’t imagine that, and i don’t have time to try it myself
thank you in advance
useful! thanks man and thanks to jquery :-)
Nice, but in FF 3.6.3 on Win7 it opened in same tab first example(short one).
Terrific. Thank you very much for sharing. I did wonder what you were supposed to do with no target attribute allowed in XHTML.
Further to Eugene’s comment above, the top example works fine for me.
Neither works for me in FF 3.6.3
$(document).ready(function() {
can be replaced with
$(function(){
Doesn’t work in IE9. Opens in the same window (same browser tab).
Sorry, I meant it doesn’t work in IE8. I haven’t tried it in IE9 yet.