Fix: async property of scripts is not assigned properly #674
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fix addresses a problem that happens when trying to set the
async
attribute of a script tag.Intuitively, setting the
async
attribute on the tag should also set the the elementasync
property. However this is not the case.This not a bug but part of the specification.
The
async
attribute is used only by the HTML/XML parser when the page is initially loaded, but it's ignored on dynamic scripts, which are async by default (you can check the section about "force-async": https://www.w3.org/TR/2014/PR-html5-20140916/scripting-1.html ).For this reason
script.setAttribute('async', value)
will not work as expected.In order to actually change the async behavior of the tag, the property must be set directly with
script.async = value
.Setting
async=false
is particularly useful to maintain the execution order of dynamic scripts.Inside issue #419 someone actually suggested this solution, but when I tried some code like the following one the tags executed out of order most of the time.
Related issues: #419