Web Development Blog

by Ei Sabai Nyo

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.
Your name:   Your email:  

8 Responses to “Open link in a new window using jQuery”

  1. examples please, i can’t imagine that, and i don’t have time to try it myself
    thank you in advance

  2. myltik says:

    useful! thanks man and thanks to jquery :-)

  3. Eugene says:

    Nice, but in FF 3.6.3 on Win7 it opened in same tab first example(short one).

  4. Derek says:

    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.

  5. cyb34 says:

    Neither works for me in FF 3.6.3

  6. Define says:

    $(document).ready(function() {

    can be replaced with

    $(function(){

  7. CSharpner says:

    Doesn’t work in IE9. Opens in the same window (same browser tab).

  8. CSharpner says:

    Sorry, I meant it doesn’t work in IE8. I haven’t tried it in IE9 yet.

Profile PicHello! 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 about me or have a look at some of the tips & resources I've written.
Subscribe to our newsletter to receive tips, resources and special offers related to web development & design.
We do NOT spam.
Your name:  
Your email:  

Tips & Resources

Tips & Resources
WordPress Web Hosting
Recommended web hosting providers for WordPress 3.0
iPhone Native App Development
Important steps into iPhone app development for beginners
iPhone Web App Development
Tips for iPhone web app development
Coupons for Web Developers
Get discounts on web hosting, domain names, templates, etc
10 Useful jQuery Snippets
Easy-to-use jQuery snippets for any website
HTML Email Newsletter
Step-by-step tutorial on how to code an HTML email newsletter
  • bluehost Hosting $6.95/month
  • Joomla Templates

Recommended Book

Categories