Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

correctly adding defer attribute to script tag with pure javascript

so I try to add a deferred script tag like this

const script = document.createElement('script');
  script.setAttribute('src', '/script.js');
  script.setAttribute('type', 'text/javascript');
  script.setAttribute('defer', true);//this is the code in question!
  document.getElementsByTagName('body')[0].appendChild(script);

But I found out the result script tag will generate the defer attribute like defer=true instead of just defer.

Are they same? What is the implication if I do defer=true rather than defer?

Thanks!

like image 555
Ezeewei Avatar asked Mar 23 '17 19:03

Ezeewei


People also ask

How do you defer a script tag?

The defer attribute is a boolean attribute. If the defer attribute is set, it specifies that the script is downloaded in parallel to parsing the page, and executed after the page has finished parsing. Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).

Should I use defer in script?

You should use defer for all other scripts. defer is great because it: Gets loaded as soon as possible — so it reduces load times. Doesn't execute until everything you need is ready — so all the DOM you need is there.

What is defer property in script tag?

The defer property sets or returns whether a script should be executed when a page has finished parsing, or not. This property reflects the defer attribute of the <script> tag. Note: The defer attribute is only for external scripts (and should only be used if the src attribute is present).

Where do I put script defer?

domInteractive. Scripts marked defer are executed right after the domInteractive event, which happens after the HTML is loaded, parsed and the DOM is built. CSS and images at this point are still to be parsed and loaded.


1 Answers

I would change it to:

script.setAttribute("defer", "defer");

They usually behave the same (although the docs technically state the value of an attribute such as defer should not be "true" or false") - or at least in any browser I've used boolean attributes in. The attribute defer is usually implemented to take effect if present in the script tag. Its value is ignored.

That being said, the spec specifies that a boolean attribute's value should not be present, or otherwise should be set to itself, with no leading / trailing whitespace (case does not matter). So, it's preferable to leave the value as the name of the attribute when setting it dynamically.

Refer to this documentation for boolean attributes (HTML5): https://www.w3.org/TR/html5/infrastructure.html#boolean-attribute

Quote from that doc:

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Note: The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

And this documentation for the defer attribute (HTML5): https://www.w3.org/TR/html5/scripting-1.html#attr-script-defer

It states:

The async and defer attributes are boolean attributes that indicate how > the script should be executed.

Update: It looks like if you set an attribute to the empty string, it will add the attribute with no value. This is also an option.

like image 179
Anish Goyal Avatar answered Oct 15 '22 15:10

Anish Goyal