Use unary HTML tags attributes with assigned value

Asked

Viewed 132 times

3

There is a difference between using attributes unários alternatively (with a value)? such as:

Standard form:

<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 identical to standard? (other attributes are also unary like disabled).

  • yes, but tested with another attribute not with the Defer

  • added an example of creating the attribute through javascript.

1 answer

2


According to the MDN, "This Boolean attribute is set to indicate to a browser that[...]" or in English "This boolean attribute is set to indicate to the browser that[...]". That is, we affirm that the attribute is boolean.

According to this user response Chuck of a similar question but Stack Overflow, relating 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. HTML uses this for Boolean Attributes, Where the presence or Absence of the attribute is what’s Meaningful, and its value is irrelevant [...] You can just write Selected."

Basically what he meant by associating with his case:

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

Confirming what Chuck describes, according to W3 regarding SGML and HTML: "Boolean Attributes may legally take a single value: the name of the attribute itself" or "Boolean attributes may legally assume a single value: the name of the attribute itself".

Still 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 in the start mark of an element implies that the attribute value 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 everything 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 them name and value, and in that case you can both pass the value of true, how much the empty value:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.