[whatwg] boolean attributes in javascript

Tab Atkins Jr. jackalmage at gmail.com
Tue Dec 8 15:22:18 PST 2009


Some clarification is in order; I didn't answer fully and was
corrected elsewhere.

There are two distinct notions of attributes here.  The first, the
"content attribute", is what you're manipulating when you do
foo.setAttribute() or foo.getAttribute().  The second, the "IDL
attribute" (may be called "DOM attribute" or "property" in some
places) is what you're manipulating when you do foo.bar (where foo is
an element and bar is an attribute name).

The content attribute can exist or not; if it exists, it's considered
'on', and if it doesn't it's considered 'off'.  It's a string, and if
specified must be one of the two values I mentioned previously.

The IDL attribute is a boolean.  It's either true or false.  Setting
it to true will set the content attribute, and setting it to false
will remove the content attribute.

So "script.async=true" is valid and does what you would expect.
Similarly, "script.async=false" is valid and does what you would
expect.  "script.setAttribute('async','true')" is invalid, but still
makes the element async.  "script.setAttribute('async','false')" is
invalid, and also makes the element async, which is not what you would
naively expect.

If you want to get more confusing, "script.async='true'" makes the
element async, because in javascript non-empty strings evaluate as
true.  However, "script.async='false'" would also make it async, while
"script.async=''" doesn't.  You have to pay attention to what you're
actually dealing with.

~TJ



More information about the whatwg mailing list