External links in new window & XHTML Strict compliant
Posted: Sat Aug 12, 2006 12:31 pm
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:
Next include in your template head section:
And finally use as follows within content on webpage:
This is based on the following article:
www.sitepoint.com/article/standards-compliant-world
Hope it helps someone out there....
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}
Code: Select all
{global_content name='External links XHTML Strict compliant'}
Code: Select all
<a href="http://www.website.com" class="external">Goto Website</a>
www.sitepoint.com/article/standards-compliant-world
Hope it helps someone out there....