{"id":25,"date":"2009-06-09T22:20:27","date_gmt":"2009-06-10T02:20:27","guid":{"rendered":"https:\/\/htmlcssjs.wpengine.com\/?p=25"},"modified":"2014-10-08T16:42:58","modified_gmt":"2014-10-08T20:42:58","slug":"html5-im-using-today-custom-data-attributes","status":"publish","type":"post","link":"https:\/\/htmlcssjavascript.com\/html\/html5-im-using-today-custom-data-attributes\/","title":{"rendered":"HTML5 I&#8217;m Using Today- Custom Data Attributes"},"content":{"rendered":"<p>I have two technology goals over the summer. <\/p>\n<p>One is to finally do some meaningful <a href=\"http:\/\/www.python.org\/\">Python<\/a> work. I&#8217;ve been sniffing around Python for a couple of years now and still don&#8217;t have any real experience with it under my belt. Hopefully I can change that this summer.*<\/p>\n<p>The other, more on-topic goal is to absolutely devour <a href=\"http:\/\/dev.w3.org\/html5\/spec\/Overview.html\">HTML5<\/a>. I&#8217;ve had my eye on it from the dawn of the <a href=\"http:\/\/www.whatwg.org\/\">whatwg<\/a>, but I&#8217;ve missed the recent opportunity we&#8217;ve been presented (in the form of browser support) to play with some of the new toys in a hands-on manner. That&#8217;ll change this summer. <\/p>\n<p>And I&#8217;ll be sharing what I find, here. <\/p>\n<p><em>See how well that works for all involved?<\/em><\/p>\n<p><!--more--><\/p>\n<p>Anyway, one of the things I immediately gravitated to when reading through some of the documentation was the inclusion of <a href=\"http:\/\/dev.w3.org\/html5\/spec\/Overview.html#embedding-custom-non-visible-data\">custom data attributes<\/a>. Obviously they&#8217;re not the sexiest new feature, but I&#8217;ve long used custom attributes in JavaScript, adding them at runtime to store all kinds of different data (for example, <a href=\"http:\/\/recruiting.foleyhoag.com\/SummerProgram\/FAQ.aspx\">the initial height of a collapsed element<\/a>, used to calculate later animations), so it&#8217;s really nice to see them wrapped into the specification. Being able to use them right in the HTML, in a standards compliant way, opens up all kinds of possibilities.<\/p>\n<p>As proof of that, I&#8217;ve already started to use them. I&#8217;ve got a project I&#8217;m working on where I need to store certain data in order to write dynamic instances of the <a href=\"http:\/\/www.permissiontv.com\/\">Permission TV<\/a> player. Without going into too much detail as to why, it&#8217;s easiest if that data is written right into the HTML from the start. Before HTML5, I would have dumped the necessary info as content into the <code>DIV<\/code> to be rewritten by <a href=\"http:\/\/code.google.com\/p\/swfobject\/\">SWFObject<\/a> and then read it out as needed. I don&#8217;t like that as is muddies the content. It comes across as nonsense to Google and to screen readers. <\/p>\n<p>I don&#8217;t like nonsense. <\/p>\n<p>Enter custom data attributes. Here&#8217;s what I came up with:<\/p>\n<div class=\"code-sample\"><code><\/p>\n<pre>\r\n&lt;ul&gt;<br \/>\r\n    &lt;li class=&quot;video&quot; data-PID=&quot;1137017&quot; data-CID=&quot;1117632&quot;&gt;<br \/>\r\n        &lt;div class=&quot;video-description&quot;&gt; This is a great video. It is so great. &lt;\/div&gt;<br \/>\r\n    &lt;\/li&gt;<br \/>\r\n<\/pre>\n<p><\/code><\/div>\n<p>In JavaScript I then use <code>getAttribute<\/code> to grab the value, and then I make the magic happen.<\/p>\n<div class=\"code-sample\"><code><\/p>\n<pre>\r\n[...]\r\nvar flashvars = {};\r\n\/\/video is a reference to the HTML object above with className \"video\"\r\nflashvars.PID = video.getAttribute(\"data-PID\");\r\nflashvars.CID = video.getAttribute(\"data-CID\");\r\n[...]\r\nswfobject.embedSWF(\"http:\/\/devkit.permissiontv.com\/Preloader.swf\", \"flash-div\", \"470\", \"266\", \"9.0.0\", \"#000000\", flashvars, params);\r\n<\/pre>\n<p><\/code><\/div>\n<p>As you can see, it&#8217;s totally straightforward. Simply use the <code>data-<\/code> prefix and you&#8217;ve got a standards compliant way to embed custom data into your pages. <\/p>\n<p>Hopefully everything about HTML5 will be that smooth.<\/p>\n<p>*I&#8217;m about to launch into a redesign of my personal site, so I&#8217;m thinking that will present the needed opportunity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have two technology goals over the summer. One is to finally do some meaningful Python work. I&#8217;ve been sniffing around Python for a couple of years now and still don&#8217;t have any real experience with it under my belt. Hopefully I can change that this summer.* The other, more on-topic goal is to absolutely [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[10],"class_list":["post-25","post","type-post","status-publish","format-standard","hentry","category-html","tag-html5"],"_links":{"self":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/25","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":0,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/htmlcssjavascript.com\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}