ieatpenguin

September 30, 2009

Emulating target=”_blank” in XHTML

Filed under: Javascript — Russell @ 3:00 pm

A friend was having trouble with this earlier today so thought I’d post the answer. Whether or not you consider this cheating (I don’t), it’s the only way to get a link to open in a new window and remain valid XHTML code, and be valid in the upcoming XHTML 4.0. It is still valid in XHTML 1.0.

The reason this was taken out in XHTML 4.0, is that the W3 see a new window as no longer being the concern of the current document – and so not valid XHTML, as therein lies the language’s concern.

Anyway, to the solution, a simple bit of Javascript:

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";
 }
}
window.onload = externalLinks;

Place this in a .js file along with the rest of your .html files, and then call it in the head of the document thus:

<script type="text/javascript" src="xhtmllinks.js"></script>

And then edit your links to call the script every time they are clicked:

<a href="http://www.r-dunn.co.uk/ieatpenguin/" rel="external">i eat penguin</a>

September 29, 2009

To blog or not to blog?

Filed under: Random — Russell @ 2:47 pm

Haven’t updated in a while, I’ve just been so busy. Not that anyone reads this crap :P .

Anyway, the DIY at the girlfriends house is almost done. I say almost, of course these things are never truly finished. A play I’m in is almost on, and even though I’m struggling with the script it will be over, one way or another with the passage of time.

And I’ve recently started a CCNA. So, even less time, but hopefully a way to “better” myself, but more importantly just learn something interesting. With any luck I’ll become an expert :) .

Powered by WordPress