Skip to content

Instantly share code, notes, and snippets.

@vovanbo
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save vovanbo/9236066 to your computer and use it in GitHub Desktop.

Select an option

Save vovanbo/9236066 to your computer and use it in GitHub Desktop.
Target Only External Links
// http://css-tricks.com/snippets/jquery/target-only-external-links/
$('a').each(function() {
var a = new RegExp('/' + window.location.host + '/');
if (!a.test(this.href)) {
// This is an external link
}
});
// http://css-tricks.com/snippets/jquery/target-only-external-links/
$('a').filter(function() {
return this.hostname && this.hostname !== location.hostname;
}).addClass("external");
// http://css-tricks.com/snippets/jquery/target-only-external-links/
$.expr[':'].external = function(obj) {
return !obj.href.match(/^mailto\:/) && (obj.hostname != location.hostname);
};
$('a:external').addClass('external');
// http://css-tricks.com/snippets/jquery/target-only-external-links/
$('a:not([href^="http://your-website.com"]):not([href^="#"]):not([href^="/"])').addClass('external');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment