External links in a new window

The 'target' attribute is deprecated and should not be used. Here is a quick and easy way to replicate that behaviour with javascript. I got the idea - well, I guess I nicked the idea - from Kevin Yank's article on Sitepoint. My script is a little bit shorter and I've cut out the explanation.

HTML
<a rel="external" href="http://tinyurl.com/rdefj">Kevin Yank's article</a>
javascript
function external() {
var as=document.getElementsByTagName('a');
 for (i=0; i<as.length; i++) {
  if (as[i].getAttribute('rel')=='external') {
   as[i].onclick=function() {
    !window.open(this.href);
    return false;
   }
  }
 }
}
window.onload=external;
Comment form

Please type the word 'sake' here:

BB code available :

  • [b]...[/b] : bold
  • [it]...[/it] : italic
  • [q]...[/q] : quote
  • [c]...[/c] : code
  • [url=...]...[/url] : url