Summary
Scripting techniques are becoming more and more popular in an attempt to work around validation errors. Scripting is incredibly useful for improving the usability of a document. Validation is relatively useful for ensuring there are no obvious errors in the document, but cannot ensure that a document is structurally correct. Does scripting around validation issues bring any real benefit?
Warning: This article includes material that is highly pedantic in nature. If pedantry offends you, or you have an allergy to pedantic material, please ensure you're completely intoxicated before reading further.
Author: Gez Lemon
Contents
- The target Attribute
- Web Standards
- Scripting around Web Standards
- Validation
- How Useful is Validation
The target Attribute
HTML 4.01 Strict and XHTML 1.0 Strict both drop the target
attribute. Authors who want to use the target
attribute to open links in a new window should use the Transitional DOCTYPE instead. Before I go any further, I should point out that this isn't a debate about opening links in a new window. I don't agree with the whole concept of opening links in a new window, but I accept that there are developers who don't like the idea of people leaving their website. Opening links in a new window isn't likely to stop people leaving, but there are plenty of developers under the illusion that it does. The point of this article isn't limited to opening links in new windows.
Web Standards
As awareness to the importance of web standards increases, thanks to the efforts of organisations like The Web Standards Project and The Web Standards Group, developers care about validation. Where possible, most developers who care about web standards use a strict DOCTYPE. If opening links in a new window is a requirement, there are techniques that can be used whilst using a Strict DOCTYPE.
Scripting around Web Standards
The first method was to use scripting to open the window when the link is clicked. As Checkpoint 6.4 of the Web Content Accessibility Guidelines 1.0 (WCAG) requires that event handlers are input device-independent (priority 2), accessibility advocates quickly updated the technique to ensure that it would also work with the keyboard, and degrade nicely when scripting isn't available.
<a href="http://somedomain.com"
onclick="window.open(this.href); return false;"
onkeypress="window.open(this.href); return false;">
Link Phrase
</a>
Developers concerned with keeping the behavioural and content layers separate, decided to attach the onclick
and onkeypress
events dynamically. Unless all links are meant to open in a new window, some means of identifying which links are to open in a new window was required. There are several solutions to this problem, but a generic solution would be to add a rel
attribute to links that open in a new window, with a value of external
. The value external
isn't a recognised link type, but can be used if an appropriate profile
attribute is used with the head
element. The fact that most authors using a rel
attribute value of external
don't use the profile
attribute is probably more pedantic than necessary for this article, even though the whole nature of this article is about pedantry.
<a href="http://somedomain.com" rel="external">Link Phrase</a>
Developers can then use the value for the rel
attribute to attach onclick
and onkeypress
event handlers to the appropriate links. A few innovative developers came up with the idea that instead of attaching event handlers, why not just add the target
attribute to the relevant links?
var objLinks = document.getElementsByTagName('a');
for (var iCounter=0; iCounter<objLinks.length; iCounter++)
if (objLinks[iCounter].getAttribute('href') &&
objLinks[iCounter].getAttribute('rel') == 'external')
objLinks[iCounter].setAttribute('target', '_blank');
The technique has the added benefit that it doesn't require a redundant event handler for the keyboard, and still degrades nicely when scripting isn't available.
Validation
If opening links in a new window is important, surely just using the target
attribute in a Transitional DOCTYPE would be the best solution? Apparently not; illustrating your firm commitment to web standards, and devising techniques to create invalid documents that pass automated validators is more important.
Online markup validators just validate the content according to the appropriate DTD; they would have no idea that a target
attribute will be added when the document is loaded to script enabled user-agents. There's nothing stopping a validator validating whatever is finally included in the DOM, but that's not the way current markup validators work.
How Useful is Validation
I've already mentioned that validators do not validate certain attribute values, and that they only check the markup, not anything added to the DOM when the document is loaded. All validators do is ensure that required elements and attributes are included, and that all elements and attributes are used according to the rules defined by the DTD. They cannot check that the most appropriate element has been used for content. Realistically, validators do very little other than ensure there are no obvious mistakes.
Validation plays a small role in ensuring that a document is structurally correct, yet developers are obviously prepared to go out of their way to ensure that a validator is happy. Tricking validators by adding invalid attributes with scripting isn't in the spirit of web standards, especially as there are perfectly valid methods of achieving the same result.
Category: Web Standards.
[scripting-away-validation.php#comment1]
"This article includes material that is highly pedantic in nature."
You should include that warning on all of your articles. They are all pendantic!
Thank you for the code to add the target attribute to links. Just what I was looking for
Posted by Strict on
[scripting-away-validation.php#comment2]
Let pedantism be the food of life!
Posted by Tim John on
[scripting-away-validation.php#comment3]
I don't think this is being pedantic at all Gez!
This technique reminds me of the early days of authors adding an 'alt' attribute to images just to avoid errors by Bobby and not worrying (or knowing) about the appropriateness of the value.
I am wondering whether anyone uses the 'id' attribute in a similar manner for other elements. I think my assessment process has just had another check added!
Thanks Gez.
Posted by Sofia Celic on
[scripting-away-validation.php#comment4]
Never have seen such an "attractive warning", it forces you to read the article
Posted by Jens Meiert on
[scripting-away-validation.php#comment5]
Validation is there to make sure the document can be parsed correctly. If the user agent *allows* the target attribute to be set, then why not take advantage of it?
If your user agent doesn't support target attribute (or ignores it because of the doctype), you're not lost anything by trying. If you're using IE or something that supports target - why not take advantage of it?
Ultimately, our web standards are there to make things the same across the board. If you can add extra functionality to those that support it, without breaking anything for those that don't, how can that be a bad thing? And moreso, isn't that the only way web standards have moved forwards thus far?
Posted by Danny Tuppeny on
[scripting-away-validation.php#comment6]
That's a good analogy, Sofia. Developers gain nothing using these techniques, other than being able to falsely claim they're valid.
Posted by Gez on
[scripting-away-validation.php#comment7]
Thank you, Jens
Posted by Gez on
[scripting-away-validation.php#comment8]
As it's you, Danny, I'll indulge you with your mini rant, even though I don't believe you really mean it
Because of the current state of most web pages, user agents are unbelievably forgiving. As far as I'm aware, no user agent would ignore the target attribute if it understands it, regardless of the DOCTYPE. The only effect a valid DOCTYPE has on a document when it's sent to the client, is to put it into standards mode if the content is served as text/html. More to the point, why not just add the target attribute directly in the markup? If you use a Transitional DOCTYPE, it would validate. If you used a Strict DOCTYPE, it wouldn't validate with a target attribute, but assuming everything else was valid, the errors would be limited to the use of the target attribute. Alternatively, if a developer really doesn't care about web standards, why bother with validation at all? So what if you've left a couple of invalid attributes in the markup?
The point of this post is that more and more people are turning to scripting to add invalid attributes (and in some cases custom elements), *purely* because they don't want them to be exposed to a markup validator. Who are they cheating? The end result is exactly the same regardless of whether they're put into the source, or added later with scripting. They've gained nothing, other than a clean validation report should someone decide to validate their document. If anything, there is a slight overhead by adding elements and/or attributes with scripting. Your very first point is that validation is to ensure that a document can be parsed correctly; adding elements and attributes through scripting isn't ensuring this at all.
First and foremost, because there are perfectly valid methods of achieving the same result. If developers insist on using things that aren't part of the spec, why bother hiding it? If people love having new windows thrust upon them as much as developers appear to think they do, why deprive those who don't have scripting enabled? Add it to the markup, and force them upon everyone.
No.
I hope you're settling into your new job okay
Cheers, mate.
Posted by Gez on
[scripting-away-validation.php#comment9]
While I agree with your post in spirit, it's not always practical to create completely pristine code without scripting. Developers and designers may be working for a company whose chosen CMS or ad-serving software creates problems, for example. Scripts like Shaun Inman's Amputator can be a real help in those situations.
See also: "Validation, Moderation, Constipation" on Mezzoblue and "Standards-Compliant, Accessible New Windows Without JavaScript" from my own weblog. The latter post deals with using XHTML's modularity to create a custom
DOCTYPE
that restores thetarget
attribute.Posted by Adam Messinger on
[scripting-away-validation.php#comment10]
There are two points being discussed in this article, and they are being slightly confused. One is whether a Web page should be able to open another browser; the other is finding ways to do it without the attribute
.
Some folks apparently don't want additional windows opened. When a link goes to another Web site, I rather like having a window open to alert me to that and to preserve the point from which I came. The author has to make a choice in this matter, as in many others.
If I choose to open another window at the risk of disturbing some users, I can do so in a number of ways. One way I can't do it is with a strict DOCTYPE and a
. Yet I may like the strict type for other reasons. (Why the target attribute no longer exists, I don't know, but the question isn't relevant to whether and how I can deal with its absence.)
While the attribute
is gone from strict HTML, the attribute
remains in the Document Object Model (DOM). There is no reason why I shouldn't use it to achieve the result I desire. I have written valid HTML, I am not "tricking" any validator, and I am not "adding [an] invalid attribute[]." I am seting a valid attribute of an element object of the DOM.
Posted by Gary on
[scripting-away-validation.php#comment11]
This article is about neither of the points you mention. It's stated clearly at the top of the article that the target attribute has been selected as an example, and that the point of the article isn't limited to the target attribute. It's aimed at developers who add invalid attributes with scripting, so that when the page is run through an online validator, it appears to validate against a strict doctype.
As I've already stated, this article isn't about people opening links in a new window. It's just a common example I decided to use to illustrate my point. People can open links in a new window if they like, but they can't choose to open a link in the same window if a developer has decided they want a link to open in a new window regardless. Authors should only concern themselves with delivering content, not how other people decide to manage their software. Forcing new windows on people who don't want them is both ignorant and arrogant, and one of the reasons it was dropped from the strict DOCTYPE.
I'm not entirely sure what you're doing, as you haven't mentioned the particular technique you're using. If you're adding a target attribute with scripting purely for purposes of validation, then you're doing it purely to trick the validator. That particular attribute is not part of a Strict DOCTYPE, so you should either be using a transitional DOCTYPE, or using a valid means of opening a link in a new window.
Posted by Gez on