Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

target= not valid in XHTML (See related posts)

Used for pages with strict doctypes (i.e. no target="_blank"). Automagically adds them back in to links with rel="external"

   1  
   2  function externalLinks() {
   3   if (!document.getElementsByTagName) return;
   4   var anchors = document.getElementsByTagName("a");
   5   for (var i=0; i<anchors.length; i++) {
   6     var anchor = anchors[i];
   7     if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
   8       anchor.target = "_blank";
   9      }
  10  
  11  }
  12  }
  13  window.onload = externalLinks;
  14  
  15  //////////////
  16  
  17  <a href="whatever" rel="external" title="whatever">Whatever</a>
  18  

You need to create an account or log in to post comments to this site.


Click here to browse all 5355 code snippets

Related Posts