Function ExternalLinks (Check the current domain)


/ Published in: jQuery
Save to your folder(s)

Get all the links and check if there's a rel attribute external, if not, compare href to the current domain name.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.