External links in new window & XHTML Strict compliant

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
Der Rudi
Forum Members
Forum Members
Posts: 56
Joined: Wed May 17, 2006 7:42 pm

External links in new window & XHTML Strict compliant

Post by Der Rudi »

Within XHTML strict the 'target' attribute for anchors is no longer valid. When one does want to use this there are a few options:
1. live with the code not being 100% valid
2. not use the target attibute, thus all external sites will be shown in the current webbrowser window
3. use some javascript to allow the usage of target attribute

I've gone for 3.  :)

First create a Global Content Block name it 'External links XHTML Strict compliant' and as follows:

Code: Select all

{literal}
<__script__ type="text/JavaScript">
<!--
//allow external link to open in new window AND valid XHTML Strict
function externalLinks() { 
 if (!document.getElementsByTagName) return; //if not supported exit; displays pages in current browser window
 var anchors = document.getElementsByTagName("a"); //get all anchors on page
 for (var i=0; i<anchors.length; i++) { //run through all anchors
   var anchor = anchors[i]; //get one anchor to test
   if (anchor.getAttribute("href") && anchor.className == "external") //if href and class=external then...
     anchor.target = "_blank"; //show the new page in a new window
 } 
} 
window.onload = externalLinks; //do the magic onload
//-->
</__script>
{/literal}
Next include in your template head section:

Code: Select all

  {global_content name='External links XHTML Strict compliant'}
And finally use as follows within content on webpage:

Code: Select all

<a href="http://www.website.com" class="external">Goto Website</a>
This is based on the following article:
www.sitepoint.com/article/standards-compliant-world

Hope it helps someone out there....
Last edited by Der Rudi on Sat Aug 12, 2006 7:17 pm, edited 1 time in total.
Post Reply

Return to “Modules/Add-Ons”