Async scripts for asm.js
async attribute like so:
<script async src="file.js"></script>
or, to do the same thing via script:
const script = document.createElement("script"); script.src = "file.js"; document.body.appendChild(script);
(Scripts created from script default to
async.) The default HTML shell Emscripten generates produces the latter.
Two common situations in which a script is *not* async (as defined by the HTML spec) are:
<script async> code(); </script>
const script = document.createElement("script"); script.textContent = "code()"; document.body.appendChild(script);
Both are counted as 'inline' scripts and get compiled and then run immediately.
What if your code is in a JS string? Instead of using
innerHTML, both of which trigger synchronous compilation, you should use a Blob with an object URL:
const blob = new Blob([codeString]); const script = document.createElement("script"); const url = URL.createObjectURL(blob); script.onload = script.onerror = () => URL.revokeObjectURL(url); script.src = url; document.body.appendChild(script);
The setting of
src rather than
innerHTML is what makes this script async.