Using jQuery to open all external links in a new window

New in jQuery here. I found several web pages that come close to what I'm trying to do, but not quite. In fact, I think it is actually assumed that it actually works, but he says:

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

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.

+5
source share
1 answer

There is no need for the @ character. Other than that, you are golden.

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

All Articles