/ Published in: jQuery
Get all the links and check if there's a rel attribute external, if not, compare href to the current domain name.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$.extend($.expr[':'],{ external: function(a,i,m) { if(!a.href) { return false; } return $(a).is('[rel*="external"]'); } }); $.fn.externalLinks = function() { return this.each( function() { $("a", this) .filter(function(){ if($(this).is(":external")){ return true; }else{ return $(this).is("[href^='http://']:not([href*='"+location.hostname+"'])"); } }) .unbind("click.external") .bind("click.external", function(){ return !window.open(this.href); }); }); };