E
E
Extramezz2016-07-03 20:44:07
JavaScript
Extramezz, 2016-07-03 20:44:07

Why doesn't defer work?

Async.js is included on the page :

<script type="text/javascript" src="async.js" async></script>

async.js content :
var script = document.createElement("script");

script.src = "defer.js";
script.defer = true;
script.type = "text/javascript";

document.head.appendChild(script);

But defer.js is not executed after the page is loaded, but as if it had an async attribute, instead of defer . (If you open the debugger, then the DOM node will be there without async, namely with defer ).
Why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-07-03
@Extramezz

For the advanced reader who knows that tags <script>can be added to a page at any time using javascript itself, note that scripts added this way behave just like async. That is, they are executed as soon as they are loaded, without preserving the relative order.
If you need to preserve the order of execution, that is, add several scripts that will be executed strictly one after the other, then the script.async = false property is used.

https://learn.javascript.ru/external-script#async...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question