In my project, window.onload
is executed before the dynamic script is loaded in IE 8, 9.
I have following codes.
index.html
<DOCTYPE html>
<html>
<head>
<script>
var eScript = document.createElement('script');
eScript.type = 'text/javascript';
eScript.src = './script.js';
var eHead = document.querySelector('head');
eHead.appendChild(eScript);
window.onload = function() {
console.log('all resource is loaded');
}
</script>
</head>
<body>
</body>
</html>
script.js
console.log('[START] script.js');
and the console output is:
IE 8, 9
> all resource is loaded
> [START] script.js
IE 10, 11
[START] script.js
all resource is loaded
I know that window.onload will run after downloading all the resources needed for the page. IE 10, 11 work as expected, but IE 8, 9 behave incorrectly.
I need an event listener that will be called after all dynamic resources (like script.js) have been downloaded and run in IE 8 and 9. I would be grateful if there is such a way.
What should I do? Thank you.
There are two matters: when the scripts are loaded, and when the document is loaded - as IE obviously for some reason doesn't take the dynamically injected scripts as part of the window at that time, while others do.
Anyway - window.onload
controls the latter (the document loading). You can control the former (the script loading) manually. Just have the last script be <script> start(); </script>
<script> start(); </script>
. As long as no scripts are defer
, this should ensure your start
function to be executed when everything is loaded. Or, you could attach eScript.onload
handlers, to know exactly when everything is done.
Alternately, and probably preferably to home-brewing it, you can use a well-known and well-tested script loader like headjs .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.