Tony didn't get any cake

  1. Home
  2. About
  3. Subscribe
  4. Archive
  5. Random

Tony didn't get any cake

Newer
Older
  • the new target”_blank”

    One of the weirder errors I got when I tried to validate my pages was for using the target”_blank” on my links.

    What am I supposed to do? Just send people away from my site everytime there is a link!?

    As it turns out the target attribute is not XHTML compliant. You just can’t use it.

    So how do you get around it?

    A little bit of Javascript helps.

    First remove all those pesky targets from your links and replace them with rel=”external”

    then create a new javascript page with this code:

    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;

    And finally you just need to link to this in the head of your document.

    using:

    <script type=”text/javascript” src=”external.js”></script>

    Now all your links with the rel external will open in new windows.

    Thanks to these guys :

    http://www.sitepoint.com/article/standards-compliant-world/

    Posted on May 29, 2009
    Comments (View)
  • blog comments powered by Disqus
  • whatidone
  • throwing-myself-into-the-world
  • brilliant-at-whatever

Field Notes Theme. Designed by Manasto Jones. Powered by Tumblr.