Is it possible to wait until all javascript files are loaded before executing javascript code?
You can use
$(window).on('load', function() {
// your code here
});
Which will wait until the page is loaded. $(document).ready()
waits until the DOM is loaded.
In plain JS:
window.addEventListener('load', function() {
// your code here
})
You can use .getScript()
and run your code after it loads:
$.getScript("my_lovely_script.js", function(){
alert("Script loaded and executed.");
// here you can use anything you defined in the loaded script
});
You can see a better explanation here: How do I include a JavaScript file in another JavaScript file?
You can use <script>
's defer
attribute. It specifies that the script will be executed when the page has finished parsing.
<script defer src="path/to/yourscript.js">
A nice article about this: http://davidwalsh.name/script-defer
Browser support seems pretty good: http://caniuse.com/#search=defer
Another great article about loading JS using defer and async: https://flaviocopes.com/javascript-async-defer/