Wednesday, July 18, 2007

XHTML open links in a new window

Now I'm a big fan of standards compliance... However one thing that I don't like about XHTML is the removal of the target="_blank" feature. I'm aware that in the run of things this isn't considered "good practise" and that the user should have the choice of how links are opened (as I do) however... Most users don't understand or apreciate this in which case if you are working on a company website and want to link to external material or sites you often don't want your users to go elsewhere, you want to keep them (even if you are providing links to other information).

I did a little research and found tis article: XHTML target="_blank" Alternative

The easiest way to apply this to my works site is to run a replace on all files replacing: target="_blank"

with: onclick=",'_blank');return false;"

Jobs a good'n


Blogger Blacksnotling said...

Nooo, inline Java Script!! Something like the below is much better for you! simply give any links you want to open in a new window a class of external.

<script type="text/javascript">
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.className == "external") {
link.onclick = clickHandler;

function clickHandler() {
return false;

ps: this is a basic example, the best way of doing this is to tie it into an event lisener. the above simply attaches an event to each link.

11:03 pm  
Blogger Blacksnotling said...

I forgot to add, you can then use CSS to style the links with a little Icon. Apply a background image to the links with a calss of external. That way there is a visible indicator of inline and external links. Wikipedia uses this to great effect.

11:15 pm  
Blogger amon said...

Those are better suggestions. But when you have 40 different pages (many written originally in HTML 4 transitional) using the old method this system is far more useful when using a command like:

rpl -w 'target="_blank"' 'onclick=",⁄'_blank');return false;"⁄' *.htm

you can do this at the same time as things like:

rpl -w '<br>' '<br ⁄>' *.htm

where there is a command line tool (that you know of) there is an hour of putting your feet up claiming to your boss this annoying repetitive task is taking ages.

5:44 pm  

Post a Comment

<< Home