W3C home > Mailing lists > Public > whatwg@whatwg.org > December 2009

[whatwg] boolean attributes in javascript

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Tue, 8 Dec 2009 17:22:18 -0600
Message-ID: <dd0fbad0912081522r2bec70cfya22b56447287ff9a@mail.gmail.com>
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
Received on Tuesday, 8 December 2009 15:22:18 UTC

This archive was generated by hypermail 2.3.1 : Monday, 13 April 2015 23:08:54 UTC