• Ok, since my site is still down and I can’t point you to the long-winded article, here’s the nuts and bolts of getting external links to open in a new browser window and still be XHTML compliant:
    As you may (or may not) know, using target=”_blank” in an anchor tag is illegal in XHTML strict. To get around this, it’s recommended you use rel=”external”. That’s all well and good, as it describes symantically what the relationship is (it’s an external link), but it doesn’t actually make the link open a new browser window.
    So, the first step in this fix is to make sure any external link that you would like to open in a new window has rel=”external” as a parameter. You could write a script that parses exteral links (similar to LaughingLizard’s Waypath Hack), or not, depending on how you want to implement. Let’s say, for example, you just want to have the links on your sidebar open in a new window.
    Find the line in your links.php file that is:
    echo("<a>
    Change to:

    echo("< a rel="external" href='$the_link'");
    Next, add the following to your extarnal javascript file (or just put it in the head section, but it's much nicer to put it in an external file along with your other scripts.)
    // begin script
    <script type="text/javascript">
    function externalLinks() {
     if (!document.getElementsByTagName) return;
     var anchors = document.getElementsByTagName("a");
     for (var i=0; i
       var anchor = anchors[i];
       if (anchor.getAttribute("href") &&amp;
           anchor.getAttribute("rel") == "external")
         anchor.target = "_blank";
     }
    }
    window.onload = externalLinks;
    // end script

    If you have more than one script that calls window.onload, or you have an onload event in your body tag, then remove the window.onload = externalLinks; line. Read this post for a short script that will load multiple onload events.
    That’s it! Any link containing rel=”external” should now open in a new window, without breaking the XHTML strict validator!
    -Tony

Viewing 9 replies - 1 through 9 (of 9 total)
  • Well, the way you describe works, but i’m really wondering if its worth to use additional javascript (and therefore cause problems for people without activated javascript) just to be 100% xhtml strict compliant. I’d rather keep using target=”_blank” until there is a solution that doesnt require javascript and say it’s 99,5% xhtml strict compliant.

    I’m using target=”_blank” and yet my pages are validating fine in XHTML.

    Thread Starter tcervo

    (@tcervo)

    Impulz:
    I was thinking about that myself, and here are/were my thoughts:
    In today’s web, surfing with javascript disabled is like driving with your engine turned off…if you’re going downhill you’ll probably get there anyway, but the rest of the time you’re out of luck.
    But, using the rel=”external” method with js turned off will simply open the link normally (in the same window), so it’s really not disabling any real functionality.
    The more I thought about it, though, the more I’m thinking I’m going to ditch all opening of links in a new window…for the same reasons I don’t use the comments popup:
    1) I personally hate popups, and new windows are essentially popups.
    2) Some people get confused when a new window opens, particularly if they run their browser full screen. Seriously, my Mom is that way. She’ll wonder why her “back” button is greyed out, and call me and say her browser is broken. I tell her (time and again) to close the browser window by clicking on the little red X in the corner, then “Wow. There’s my other page! Where did that come from?”
    3) New windows and/or popups wreak havok on screen readers. The switch in focus often confuses those using a screen reader since they are genearlly unaware that a new window has opened. I doubt anyone with a screen reader is visiting my site, but that’s not the point. (Side note: we had this conversation at work, and it turns out a member of our board is blind, and visits the company website with a screen reader…Luckily my argument had already prevailed.)
    Ok, I’ll get off my soapbox now.
    -Tony

    jackiefg, your pages are validating as XHTML 1.0 Transitional, not XHTML 1.0 Strict or XHTML 1.1.

    This might be a simpler solution than the rel=”external” and externalLinks() function.
    For a and area tags, just replace target=”frame-target” with onclick=”target=’frame-target’;”. The onclick event handler is valid under all flavors of XHTML and the script itself is JavaScript 1.0 compatible. If JavaScript is unavailable or disabled, the hyperlink will open in the same frame.
    Example:
    Open in an new, unnamed window.
    For form tags, set the target property of your form. The script is JavaScript 1.0 compatible. If JavaScript is unavailable or disabled, the results of the form submission will open in the same frame.
    Example:
    <form id="myform">
    </form>
    <script type="text/javascript">
    myform.target = "_blank";
    </script>

    I haven’t found a simple solution for base or link tags. Any suggestions would be welcome.

    how about a checkbox for allowing the visitors to decide if they want the links to open in new windows?
    how do we do this?

    Here’s a good alternative:

    https://www.texastar.com/tips/2004/target_blank.shtml

    but still throws up more support issues, but I guess if you want to use xhtml 1.1, then this is my preferred method…

    The above link basically shows you how to include the target module in xhtml 1.1

    Isn’t that javascript solution a bit hypocrite? Because, in truth you are still using target=”_blank” you just hide it from the validator. There are other ways to open a new window.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘XHTML valid new windows’ is closed to new replies.