Use unary attributes of HTML Tags with assigned value

3

Does it make a difference between using the unários attributes in an alternate way (with a value)? such as:

Standard shape:

<script src="/js/" defer></script>

Alternative Form 1:

<script src="/js/" defer="defer"></script>

Alternative form 2:

<script src="/js/" defer=true></script>

Alternative Form 3:

<script src="/js/" defer="true"></script>

Will it run identically to default? (other attributes are also unary as disabled ).

    
asked by anonymous 12.11.2015 / 12:53

1 answer

2

According to the MDN , "This Boolean attribute is set to indicate to the browser that [...] "or in Portuguese" This Boolean attribute is set to indicate to the browser that [...] ". That is, we say the attribute is Boolean.

According to this answer from the user Chuck of a similar question however in Stack Overflow , referring to attribute selected :

"In SGML, an attribute may be minimized so that its value alone is short for both the name and the value, with the only possible value for the attribute in this case obviously being the attribute's own name. attributes, where the presence or absence of the attribute is meaningful, and its value is irrelevant [...] You can just write selected. "

Basically what he meant by associating with your case:

In SGML (Standard Generalized Markup Language), an attribute can be minimized to the value alone when name and value are the same , in which case you obviously use only the name attribute (in your case, use only defer ). HTML uses this form for Boolean attributes where the presence of the value is not irrelevant (in the case of true or defer ). That is, you can just type defer .

Confirming what Chuck describes, according to W3 name of the attribute itself.

p>

In the same document: "Some attributes play the role of boolean variables [...]. Their appearance in the start tag of an element implies that the value of the attribute is true .

"Some attributes play the role of Boolean variables [...] Their appearance at the start mark of an element implies that the value of the attribute is true .

That is, when the element is declared it is the same as saying that its value is true :

<script src="/js/" defer></script>

<script src="/js/" defer="true"></script>

<script src="/js/" defer="defer"></script>

<script src="/js/" defer=""></script>

All these statements are valid, but according to all that has been said above it is recommended to use the first alternative.

An interesting fact is that when using javascript, but precisely the setAttribute to create these attributes you need to pass two parameters being name and value , in which case you can either pass the value of true , or the empty value:

setAttribute('defer', ''); or setAttribute('defer', 'true');

A hint, for HTML attributes use value always within "", this is the default form of the language.

    
12.11.2015 / 13:38