Snippets, xHTML

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;
    });
});

8 thoughts on “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. Nice, but in FF 3.6.3 on Win7 it opened in same tab first example(short one).

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

  4. Neither works for me in FF 3.6.3

  5. $(document).ready(function() {

    can be replaced with

    $(function(){

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

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

Comments are closed.

Twitter
LinkedIn
YouTube
Instagram