> Because IE munges attributes and properties, you should only ever use
> DOM properties for HTML atributes.
This is (as I understand it) one part of the rationale for the `data-`
prefix: There aren't any DOM element properties with those names, and
so IE's broken behavior isn't an issue with them. Yes, it does dump
them on the element instance (if you put a "data-foo" attribute on a
div, the element instance for that div will indeed have a property
called "data-foo" on it -- prior to IE9), but it's harmless (though as
always, YMMV).
For instance, try this:
http://jsbin.com/ewade3/2
That works fine on IE6, IE7, (I don't have IE8 handy), IE9, Chrome 10,
Safari 5, Firefox 3.6, and Opera 11 under Windows; and Chrome 10,
Firefox 3.6, and Opera 11 under Linux (Ubuntu 10.04 LTS). I don't have
a Mac handy, but it works in Mobile Safari on my iPhone. :-) The "IE
check"s show that IE6 and IE7 and presumably IE8 (but not IE9, yay) do
dump the "data-" properties on the element, but you wouldn't look for
them there anyway since no one else does -- stick to `getAttribute`
(or better yet, Prototype's `readAttribute` because of the *other*
insane things IE does with attributes) and you're fine.
> > HTML5 lets you do this, and pretty much anything else you like, by
> > adding a data- prefix to the attribute name. Have at it.
>
> HTML5 is not a standard, nor is it widely supported yet.
True. But there are two very different aspects to HTML5: Codifying and
standardizing the things browsers were already doing and had been
doing forever, and defining new things for them to do. By its very
nature, the first part is widely supported. :-) "data-" attributes
fall into that category (every browser I've ever seen supported custom
attributes on elements; HTML5 reins it in a bit). I dare say that that
subset of HTML5 is a better specification for HTML in the real world
than the HTML4.01 standard from over 11 years ago. Of course, the
trick with the HTML5 spec is knowing which bits are which. ;-)
-- T.J.