Reputation: 148534
I create a script element (document.createElement('script')
....) I set its async property by: x.async=true
Which means that the scripts are not blocking and the execution order between each other - is not preserved ( as opposed to defer
(which also waits for dom to load.))
Question :
But if I set the async attribute - does it mean that I must use document.ready ? because I could run a method which is declared in the script file which is future to be downloaded - hence error.
Upvotes: 3
Views: 159
Reputation: 3008
From personal experience, it is better to use document ready or window load in the async script file just to be safe.
This is because after the first load, the JavaScript file would be cached by the browser, and depending on the browser/circumstances, can load from cache and try to execute before the DOM is ready.
Upvotes: 2