0

Target Blank for XHTML

Target Blank for XHTML via @largestartist Posted by Jose Cuadra on April 21, 2009 Tags: | | | Filed Under: References

Given that the target attribute for the anchor tag does not comply with the W3C recommendation, which in turn does not validate, creating a new window for a link is not possible using a Strict XHTML Doctype.

Solution

Instead of changing the Doctype you may use JavaScript to open a new window. Writing a script for each anchor link would be too time consuming so we automate the process by grabbing all the Anchor links and then looping through them to see if they contain the “targetBlank” class.

JavaScript

function int(){
//Create an Array with all the Anchor Nodes
var anchors = document.getElementsByTagName("a");
//Loop through all the Anchor Nodes
for(var i = 0; i < anchors.length; i++){
//If an Anchor Node contains the string "targetBlank" in it's class then assign the onclick event to it.
if(anchors[i].className.search(/targetBlank/) != -1){
anchors[i].onclick = function(){
var href = this.getAttribute("href");
var blankWindow = window.open(href, "");
blankWindow.focus();
return false;
}
}	
}
}
window.onload = int;

CSS

This CSS is not required to make this entire solution function correctly. Simply for aesthetics the style sheet provides a small popup image to the right of each “targetBlank” link.

a.targetBlank{
padding-right:20px;
background-repeat:no-repeat;
background-position:right center;
background-image:url("../imgs/popIcon.png");
}

XHTML

All that you need is to apply the name “targetBlank” class to an anchor link.

<a class="targetBlank" href="http://www.google.com">A new page to Google</a>

Demo

Download

Want More? Try These.

Related Articles
Multiple On Load Events
fliteBox Lightbox for Flickr
The Coolest Lightboxes Around
JavaScriptless Tool Tips
CFCACHE, IE, CSS and XHTML
Disable Safari’s Textarea Grip
Type Doc Re-Released
Flash Slideshow in Actionscript 3
Access Cascading Styles with JavaScript
JavaScripted Tooltips
Random Articles
Funny Superman Shirts
Access Cascading Styles with JavaScript
Random Functions
The Definitive List of Vector Art
JavaScriptless Tool Tips
From Work Path to Shape Layer
IE Conditional Comments
Table Behavior without Tables
Disable Safari’s Textarea Grip
Web Fonts Suck!

Leave a Reply