How to make a link open in a new window with XHTML
The old-fashioned method of opening a link in a new window is to use the attribute target="_blank". Unfortunately, if you use an XHTML validator, you'll find that this attribute has been deprecated. Originally it was proposed to use the 'rel' attribute, like this: rel="external". Although this validates, it doesn't cause the browser to open a new window.
So, here's a way using JavaScript to have valid XHTML, and have your links open in a new window! Just make sure your links have the rel="external" attribute set, then add this JavaScript code to the <head> section of your web page. It will loop through your links, and dynamically add the old target="_blank" to the element, but this is on the client-side, so no errors in validation are caused.
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
If you're going to use this method throughout your site, it would make sense to place this code in an external .js file, and reference it in the <head> if you page, like this:
