способ 1

1
2
3
$('a').filter(function() {
   return this.hostname && this.hostname !== location.hostname;
}).addClass("external");

способ 2

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

способ 3

1
$('a:not([href^="http://your-website.com"]):not([href^="#"]):not([href^="/"])').addClass('external');

способ 4

1
2
3
4
5
6
$('a').each(function() {
   var a = new RegExp('/' + window.location.host + '/');
   if (!a.test(this.href)) {
      // Внешняя ссылка
   }
});

источник: css-tricks.com

Оставьте свой комментарий