Posted By

touffies on 11/12/10


Tagged

jquery externalLinks


Versions (?)

Function ExternalLinks (Check the current domain)


 / 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.

  1. $.extend($.expr[':'],{
  2. external: function(a,i,m) {
  3. if(!a.href) { return false; }
  4. return $(a).is('[rel*="external"]');
  5. }
  6. });
  7.  
  8. $.fn.externalLinks = function() {
  9. return this.each( function() {
  10. $("a", this)
  11. .filter(function(){
  12. if($(this).is(":external")){
  13. return true;
  14. }else{
  15. return $(this).is("[href^='http://']:not([href*='"+location.hostname+"'])");
  16. }
  17. })
  18. .unbind("click.external")
  19. .bind("click.external", function(){
  20. return !window.open(this.href);
  21. });
  22. });
  23. };

Report this snippet  

You need to login to post a comment.