[whatwg] boolean attributes in javascript

Brian Kuhn bnkuhn at gmail.com
Tue Dec 8 11:49:45 PST 2009


How do I correctly set a boolean
attribute<http://dev.w3.org/html5/spec/Overview.html#boolean-attributes>on
a DOM element object in Javascript?

var script = document.createElement('script');

   1. script.async = true;        // makes the most sense, but appears go
   against the spec
   2. script.async = 'true';       // worse, but not bad, but also appears
   to go against the spec
   3. script.async = '';            // sets to empty string, but what does
   that really mean?
   4. script.async = 'async';   // sets async = async, which is weird
*and*verbose

And then you have the debate on setting the property directly versus using
setAttribute.  Any thoughts on that?


To me, boolean attributes seem to break the rule of least
surprise<http://en.wikipedia.org/wiki/Principle_of_least_astonishment>.
 I find it very hard to believe people will understand:
<script async="" src="..."></script>    or   <script async="async"
src="..."></script>

more than:
<script async="true" src="..."></script>

Thanks,
    Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.whatwg.org/pipermail/whatwg-whatwg.org/attachments/20091208/9c16ac4c/attachment.htm>


More information about the whatwg mailing list