XHTML Strict and the <a> "target=" attribute

S

(Shovel)

Guest
Right then, was validating my new web page (http://www.ben-ward.co.uk) and got thrown back that using the target=" " tag in hyperlinks was depreciated.

Does anyone know why? I just can't see any good reason for it...?

It does also appear to offer the rather silly solution of having to use javascript to make a new window to keep things valid.

Anyone shed any light on this?
 
F

FatBusinessman

Guest
The apparent reason for this is that "people will get confused when the Back button doesn't work in a new window".

Which is, to be frank, a load of crap.
 
S

(Shovel)

Guest
Thanks a tip off from a highly regarded but sadly absent member of this forum, I have an answer.

It is to do with XTHML not being responsible for behavior, just as it is not responsible for layout.

So, the solution is indeed JavaScript, for which there are various solutions on the web, using the DOM to pick out links. You can use the 'rel="external"' attribute to identify items if you wish, or do as I suggest below:
Code:
function blankLinks() {
 if (!document.getElementsByTagName) return;

 var links = document.getElementsByTagName("a");
 for (var i=0; i<links.length; i++) {
   var link = links[i];
   if (link.getAttribute("href").match("http://"))
     link.target = "_blank";
 }
}
window.onload = blankLinks;

This script picks out all the hyperlinks containing "http://" in the address, therefore any external links will automatically be openned in a new page - which internal links (being relative to the domain) will not be affected.

That code just gets called from somewhere in the <head>
 

Users who are viewing this thread

Similar threads

W
Replies
8
Views
1K
wyrd_fish
W
P
Replies
4
Views
689
LazyJim
L
M
Replies
4
Views
598
W
T
Replies
6
Views
767
(Shovel)
S
W
Replies
1
Views
920
MYstIC G
M
Top Bottom