I need to load babel-polyfill only for IE. Also it's required to make IE wait and do not perform the console.log('Checkpoint 2');
and code below it (including IeOnlyPolyfillTest.js) until babel-polyfill will be fully loaded from the external resource.
<body>
<script type="text/javascript">
var userAgent = window.navigator.userAgent;
var isInternetExplorer = userAgent.indexOf('Trident/') > 0 || userAgent.indexOf('MSIE ') > 0;
if (isInternetExplorer) {
var htmlDocumentHead = document.getElementsByTagName('head')[0];
var babelPolyfillScript = document.createElement('script');
babelPolyfillScript.type = 'text/javascript';
babelPolyfillScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.0.0-rc.1/polyfill.min.js';
console.log('Checkpoint 1');
htmlDocumentHead.appendChild(babelPolyfillScript);
console.log('Checkpoint 2');
}
</script>
<script type="text/javascript" src="scripts/IeOnlyPolyfillTest.js"></script>
</body>
And IeOnlyPolyfillTest.js part:
(function launchApplication() {
console.log('Checkpoint 3');
testSymbolFeature();
})();
function testSymbolFeature() {
let symbolTest = Symbol('foo');
console.log(`Symbol feature is working! ${symbolTest.toString()}`);
}
First, I cannot add async="false"
attribute:
babelPolyfillScript.async = false; // no effect
babelPolyfillScript.async = 'false'; // async="" will be generated
I tried following following solution (original answer):
var script = document.createElement("script");
script.src = "script.js";
console.log("a");
document.body.appendChild(script);
console.log("b");
In my case, logs order is always right too, however if to reload page some times, we randomly get cases with error and cases without error (by other words, error conditions is unclear, and it occurs case by case):
Bad case:
Good case:
So, what we can do to make console.log('Checkpoint 2');
and all scripts below wait until babel-polyfill will be fully loaded?