Jeff Lamb
Jeff Lamb

Reputation: 5865

Using jQuery to open all external links in a new window

New to jQuery here. I've found several web pages that come close to what I'm trying to do, but not quite. Actually, I think the following is supposed to actually work, but it's saying:

[@href^="http://"]

is not recognized (syntax error). Any help?

$(document).ready(function() {
    $('a[@href^="http://"]').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target', '_blank');  
});

Thanks.

Upvotes: 5

Views: 2523

Answers (1)

btelles
btelles

Reputation: 5420

No need for the @ symbol. Other than that, you're golden.

$("a[href^='http://']")...

Upvotes: 9

Related Questions