Is it possible to wait until all javascript files are loaded before executing javascript code?

javascript, jquery

Solution

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
})

Problem

We have several JavaScript files which we load at the bottom of the master page. However, I have the situation that I need to perform some JavaScript before the other scripts are loaded. Is it possible to wait till all the JavaScript files are loaded and then execute some JavaScript code? I thought `$(document).ready()` did this, but as it turns out, it doesn't. Of course we can move the script files from the bottom to the top, but I am wondering if it's possible what I want.

Original source

Related problems