Forum Home
Press F1
 
Thread ID: 87785 2008-03-04 03:37:00 Links in XHTML vs HTML question Tony (4941) Press F1
Post ID Timestamp Content User
646160 2008-03-04 03:37:00 In HTML you can get a link to open in a new window/tab by using the target="_blank" attribute. In XHTML 1.0 Strict this is not allowed, but I can't find anywhere how you are supposed to achieve this result using legitimate XHTML and/or CSS.

Any XHTML gurus out there who can point me in the right direction? :help:
Tony (4941)
646161 2008-03-04 05:04:00 The only real way is using JS to unobtrusively add the target attribute to links you would like to open in a separate window/tab. The JS usually involves running through every <a> element and if the "rel" attribute contains 'external' then add a target="_blank" to it. Script below -

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;

(I know it's not PHP but the syntax highlighting still applies basically) See the SitePoint article on this here (www.sitepoint.com). However if JS is disabled, well I'm sure those people wouldn't expect half the internet to work so they wouldn't be fussed.
sal (67)
646162 2008-03-04 06:15:00 I guess the question that immediately arises is : Why? We seem to have gone from a dead simple way to achieve a new window, that would work under all circumstances, to one that requires at least a bit of JS knowledge by the coder (never essential before) and that the user has JS enabled in their browser!

Do you know why the target attribute was removed from XHTML?

OK, rant over - thanks for the info, and the link. I guess I'll just have to bite the bullet and write the code.:)
Tony (4941)
646163 2008-03-05 01:21:00 Do you know why the target attribute was removed from XHTML?My understanding is that xhtml was designed to work on anything, so I imagine that the target attribute doesn't always work so they removed it... does anyone actually use xhtml strict? There are some things that IE uses that the other browsers don't, but those are usually easy to avoid, and you'd still be able to do a lot more than xhtml strict will allow (and still display properly in all the common browsers).

Mike.
Mike (15)
646164 2008-03-05 02:52:00 Does anyone actually use xhtml strict? I'm just at the end of building a new website from scratch, so with no baggage, I thought that strict adherence to standards was probably a good thing, so I chose to use xhtml strict - without, I must say, a total understanding of the implications :) - hence my hassle with target=...

I suppose I could change to transitional; I guess the term" transitional" tends to imply "temporary", so I've been trying a bit of future-proofing.

Everything seems to render OK if I do use target=, it just gives me messages if I try to validate the code.
Tony (4941)
646165 2008-03-05 08:06:00 We have to use XHTML strict for our university website project this semester. Going to be interesting... beeswax34 (63)
646166 2008-03-06 10:47:00 You can do this easily with a bit of Javascript in the link itself:



<a href="pressf1.pcworld.co.nz onclick="window.open(this.href); return false">PC World</a>


This way, the link works as "normal" if Javascript is not enabled in the client browser.

The question is -- why do you want a new window opening? I prefer to leave the user in total control of windows, as I hate having pages spawn new ones ...
davehartley (3487)
646167 2008-03-06 11:59:00 davehartley: What you posted is achieved unobtrusively by the JS I posted earlier. sal (67)
646168 2008-03-06 12:23:00 I realise that -- however, putting the Javascript in an onclick event

reduces the amount of Javascript, thus making it less error-prone for non-programmers
is "correct" XHTML Strict rather than "fooling" the validation by adding attributes via the DOM once the page has loaded


Just offering an alternative :)
davehartley (3487)
646169 2008-03-06 17:44:00 You can do this easily with a bit of Javascript in the link itself:



<a href="pressf1.pcworld.co.nz onclick="window.open(this.href); return false">PC World</a>


This way, the link works as "normal" if Javascript is not enabled in the client browser.

The question is -- why do you want a new window opening? I prefer to leave the user in total control of windows, as I hate having pages spawn new ones ...That looks like what I want. I understand what you are saying about new windows, but I think it depends on the circumstances.

Thanks for the help.
Tony (4941)
1 2