DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Target= Not Valid In XHTML

05.30.2007
| 4324 views |
  • submit to reddit
        Used for pages with strict doctypes (i.e. no target="_blank"). Automagically adds them back in to links with rel="external"

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
     anchor.target = "_blank";
    }

}
}
window.onload = externalLinks;

//////////////

<a href="whatever" rel="external" title="whatever">Whatever</a>