XHTML valid new windows
-
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") && 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
- The topic ‘XHTML valid new windows’ is closed to new replies.