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

Comments are closed.

Career Guide for Software Developers