[whatwg] boolean attributes in javascript

Garrett Smith dhtmlkitchen at gmail.com
Tue Dec 8 15:04:04 PST 2009


On Tue, Dec 8, 2009 at 1:41 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
> On Tue, Dec 8, 2009 at 1:49 PM, Brian Kuhn <bnkuhn at gmail.com> wrote:
>> How do I correctly set a boolean attribute on a DOM element object in
>> Javascript?
>> var script = document.createElement('script');
>>
>> script.async = true;        // makes the most sense, but appears go against
>> the spec


HTML content attributes and dom properties are different things.

Like the majority of cases, DOM properties make sense here. The async
property is specified in HTML5, which copied the property from IE.

W3c specs use the term "attribute" to mean "dom property". DOM
properties are not HTML content attributes.

[...]

>
> Setting an attribute foo to '' or 'foo' is the correct way.

getAttribute/setAttribute is not any more correct than DOM properties
but it is less interoperable.

Some content attributes "reflect" object state, others do not. Which
attributes "reflect" varies between browsers and the attribute in
question.

Setting an attribute to the empty string may have the effect of
removing the attribute in some browsers.

Garrett


More information about the whatwg mailing list