jQuery External Link Selector

I always lose this script, so I'm saving here for future use. It grabs every link, with a supplied href that has a domain seperate from the current location. This differs slightly from this version which doesn't check to make sure that something is supplied with the href, thus it will catch any a tag, even if its just a named anchor. (yes you could just use $(a[href]:external) but then I have to remember to do that everytime).

// external link selector
// $('a:external').addClass('external');
$.expr[':'].external = function(obj){
    return (obj.href != undefined)
        && (obj.href != '') 
        && !obj.href.match(/^mailto\:/)
        && (obj.hostname != location.hostname);
};

Aron Duby

Web Developer, Polo Enthusiast, Professional Photo-Bomber, Like Neo from 'The Matrix', but less terrible... more