delarge
1/29/2014 - 8:44 PM

Automatically add title attribute to external links e.g. Visit theonion.com...

Automatically add title attribute to external links e.g. Visit theonion.com...

// our own domain
var domain = window.location.hostname;

// This will add rel="external" to all external links
//comment this line out to add rel="external" manually
$("a[href^='http:']").not("[href*='" + domain + "']").attr('rel','external');

// for each rel=external link
$('a[rel="external"]').each(function() {

  // Get the href attribute
  var url = $(this).attr("href");
  
  // Strip http:// & www. from url
  var url = url.replace("http://", "");
  var url = url.replace("www.", "");
  
  // Strip trailing slash
  var url = url.replace(/\/$/,"");
  
  // Set length of url before trimming
  var limit = 30;
  
  // Get length of url
  var length = url.length;
  
  if(length > limit){
  var url = url.substring(0,limit);
  var url = url + "...";
  }
  
  // Add the new title
  $(this).attr('title', "Visit " + url + "");

}); // end for each