Correctly adding defer attribute to script tag with pure javascript

so i am trying to add a pending 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 that the script tag will generate an attribute defer, for example defer=true, and not only defer.

They are the same? What is meant if I do defer=true, and not defer?

Thank!

+4
source share
1 answer

I would change it to:

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

( , , defer, "true" false ") - , , . defer , script. .

, boolean , / ( ). .

(HTML5): https://www.w3.org/TR/html5/infrastructure.html#boolean-attribute

:

. , .

, , ASCII-- , .

. "true" "false" .    ,    .

defer (HTML5): https://www.w3.org/TR/html5/scripting-1.html#attr-script-defer

:

, , > script .

+4

Source: https://habr.com/ru/post/1673051/


All Articles