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
- The target Attribute
- Web Standards
- Scripting around Web Standards
- 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.
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
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
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.
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.