views:

314

answers:

4

Is there a way to have non-standard attributes like

onselectstart
oncontextmenu 
...

in a tag, and still pass validation as HTML 4.01 transitional somehow?

Apart from adding the properties later on using Javascript.

+1  A: 

No, you would have to change the doctype.

<!DOCTYPE HTML>

That doctype will allow you to use your own attributes. Heres a good article on the matter

Ben Shelock
This only lets you use custom attributes that begin data- — while the example uses proprietary attributes that can't be represented that way. The question also specifies HTML 4.01 Transitional (which HTML5 is not).
David Dorward
Which is why I began the question with no but specified an alternative.
Ben Shelock
@Ben: Reread David's comment. He's saying your alternative is wrong.
Chuck
+2  A: 

No, it isn't. There is no scope for adding things to the language while still conforming to the language.

David Dorward
+3  A: 

Using those attribute will produce an invalid document.

Adding those attributes later using Javascript will produce an invalid document (even if the W3C validator is unable to tell you so).

But W3C has never been against using proprietary extensions. Validation should not be a requirement. It is a way to tell you when you do not conform to the spec. W3C will not send the FBI just for an invalid page.

If you are relying on proprietary extensions to give your visitor a better experience (but not rely on it) then you are on the good path :-) Just pray (or contribute) for those to be in the next spec.


Now if it is about preventing browser context menu or selection, that's just rude! Don't do it!

Vincent Robert
I have begun to care about validation even in the back-end because it makes it easier to detect errors. For a proper workflow, I want to see a green (validator) light somewhere when there is nothing to fix, and a red light when there is. That's why I want to remove even the smallest invalidity. The "oncontextmenu" is totally innocent and bound to a single DIV, don't worry :)
Pekka
I never meant to make you feel guilty of having invalid code. You seem to be using validation in the right way: detecting coding errors. Now for your specific problem, your back-end validation could have specific cases for those attributes and just issue a warning? That will allow to "half-way pass" while still detecting real errors.
Vincent Robert
By real errors, I meant important errors like invalid structure or misspelled attribute which might really mess rendering up and make you lose time.
Vincent Robert
+3  A: 

While you can't add your own tags or attributes in HTML 4.01, a common technique is using standard HTML tags or attributes to include your information, even if it isn't exactly the correct usage according to the spec. For example, the 'class' attribute can store almost any kind of data:

<a href="#" id="user-link-1" class="username:matt email:[email protected]">Matt</a>

You can retrieve the class of the link above and split the 'class' attribute up to retrieve your data.

Some other tags and attributes I've seen used for custom data: <script> tags with a non-JavaScript 'type' value, hidden input values, the 'title' attribute on various tags.

You have a couple of other options if you don't mind changing from HTML 4:

You can also add custom attributes to your HTML document at runtime via JavaScript. For example:

var body = document.getElementsByTagName("body")[0];
body["my-attribute"] = "Hello, world!";
alert(body["my-attribute"]);

This can be helpful if your information is dynamic and doesn't need to be stored in the markup at all.

Matt Ryall
Thanks, but in my case it is about (IE) specific tags that enhance user experience, so I can't store the values in other attributes.
Pekka