The HTML5 Data- Attribute: Feed content to scripts
Join the DZone community and get the full member experience.Join For Free
for most of you the new html5 data- attribute should be quite familiar by now. it was one of the first additions of html5 to become available right away. no problems with backwards compatibility, no browsers/browser versions being a pain in the ass, just clear and easy to use. but it's just not a very sexy addition, which is why you'll only read about the property's best practices in more specialized places. a real shame, as it is an important step in further cleaning up our html code.
what and why?
quote custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.
these attributes are not intended for use by software that is independent of the site that uses the attributes. unquote
that's what the spec has to say about the data- attribute. basically it's an attribute (a label/value pair) that holds data which doesn't belong in the html as a text node. this data is not intended for users, it's there to aid scripts in their tasks. following the "data-" prefix is a string that can be freely chosen (within the boundaries of regular xml constraints), allowing multiple data- attributes with different suffixes to be placed on one single html element.
before the data- attribute was introduced to the spec we had to be creative when script-only data was needed in our html code. we abused hidden input fields, we hid html elements from view with css, we even used title attributes to stuff our script data and the really adventurous amongst us just made up their own attributes, extending the dtd if validation was a requirement. none of these methods were perfect, some were just plain hacks or failed to work in all circumstances. an standard alternative was needed and so the data- attribute was born.
the above explanation has been quite theoretical so far, so let's find some real-life use cases for our attribute. these past months i've been able to distinguish three main use cases where the data- attribute proved to be an invaluable tool. as an example, let's take a run of the mill web shop and see where our data- attribute comes in handy:
1. data for computations in scripts
< .. data-quantitystep="100" .. >
adding products to your basket can be somewhat tricky depending on what you're trying to sell. if you are an online media shop things should be quite straightforward, but if you sell products that are sold in different unit quantifiers (like a 6-pack of coke vs 500gr of cheese) than there are more things to consider. if you use a custom +/- control to change your quantities, it will behave differently for different products. for a 6-pack of coke you could simply up the quantity by 1, but for the cheese you might opt to up the quantity in steps of 100gr at a time.
this value differs depending on the product people are trying to add to their basket, so instead of sticking the quantity step data in hidden input elements or making a lengthy passage through the back-end every time you add a product, we can now add this value as a data- attribute and have our scripts use that.
2. data for changed state
< .. data-replace="close extra information" .. >
using the data- attribute though, we can have the changed state string in our html without it ever showing up. the script can extract the data from the attribute when needed and can substitute the original value back into the data- attribute (anticipating the next state-change of the component).
3. help your back-end developer
< .. data-productid="025652156" .. >
and ultimately the data- attribute can even be used to help out your back-end developers a little. sometimes it's easier to just add extra meta data to your html code, facilitating ajax calls and other back-end operations. if you can include the database product identifier for each product in the html, ajax calls handling a product could be made quicker and easier as the product identifier can be passed on, instead of going through a few extra loops and queries on the back-end side trying to find out which product is being added.
internal vs external
if you check the spec again, you will also notice that the data- attribute is only meant for data that is to be used by internal scripts, meaning scripts you have specifically developed to work on the website you're building. a strange limitation that begged for further explanation, but none was given in the spec itself. my first guess was that they included this to prevent abuse of the data- attribute (seo keyword stuffing that would be picked up by search engines) but that felt like a big price to pay for something that cannot be stopped either way.
so i went to the whatwg (irc ftw! - check the logs ) and asked around for more concrete information. it turns out that it's not really an issue of abuse, but of possible conflict. since there is no governing entity and everyone can freely decide the name of the attribute, collisions might occur. google might be using data-contentid for one thing, while amazon might be using the same attribute for something else.
fair enough, but that doesn't solve our problem when we need to provide extra data for external scripts. i pushed the whatwg for alternatives and even though their first options were all less than satisfactory (using microformats - meaning you still need to add your data as text nodes - or using the itemid property - meaning you're limited to only one property), there is one way to work around the scope limitations of the data- attribute:
<div itemtype="..."> <meta itemprop="productid" content="025652156" /> </div>
apparently there's an edge case where meta elements can be used outside of the head of a html document. when combined with an itemprop attribute they serve the same purpose as data- attributes, but for data targeted at external scripts. this was the first i ever heard of it, but all in all it's a decent solution that fits the whole microdata implementation worked out in html5.
the only problem of course is that this is way more complex than introducing some or other data- attribute to hold your data, as you need external documentation for your microdata semantics and structure. figuring this out as a front-end developer is hard enough, getting your back-end developers on board is a completely different challenge. i fear that the cost of microdata is just too high too make this a very workable solution, especially when nothing is holding you back to just use a data- attribute. and if you choose your attribute name wisely (what's the chance of data-amazonproductid ever appearing on some site not intended for amazon?) there shouldn't be much of a problem.
as for the microdata alternative (external scripts), i'm still not too sure. i'd be inclined to ignore it for the time being, hoping that we'll once again face a "pave the cow path" situation in future releases of the html spec.
Published at DZone with permission of Niels Matthijs, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.