Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 860
Martijn Avatar asked Jun 29 '12 07:06

Martijn


People also ask

Does the order in which we load our JS files matter?

If I'm understanding your question I think you're asking if it matters where in a file a function/method is defined, and the answer is no, you can define them anywhere in a single source file. The JavaScript parser will read in all symbols before trying to run the code.

Does JavaScript run before page load?

You can run javascript code at any time. AFAIK it is executed at the moment the browser reaches the <script> tag where it is in. But you cannot access elements that are not loaded yet.

When should I load JavaScript?

Because of the fact that browsers have to pause displaying content of a page when it's parsing a Javascript file, the recommendation is to load the Javascript at the bottom of the page to speed up displaying a page's content.

How do I make sure JavaScript is loaded?

What is the best way to make sure javascript is running when page is fully loaded? If you mean "fully loaded" literally, i.e., all images and other resources downloaded, then you have to use an onload handler, e.g.: window. onload = function() { // Everything has loaded, so put your code here };

How long can you wait before executing a function in JavaScript?

In JavaScript, we can wait 5 seconds before executing a function or some code with the use of the setTimeout method. Skip to primary navigation Skip to main content Skip to primary sidebar The Programming Expert Solving All of Your Programming Headaches HTML JavaScript jQuery PHP Python SAS VBA About

Does defer script wait for all other JS files to execute?

It does not seem to wait for all other JS files to have executes, at least not in Firefox. Like the doc says, defer script loads when the page is parsed (instead of when js/css is loaded). This ain't a solution. Works very well with both async and defer while loading on scripts.

How to wait 5 seconds in JavaScript?

We will use JavaScript to wait 5 seconds using the setTimeout() method. While we are waiting for the 5 seconds, we will provide the user with a countdown by using the setInterval() method. The setInterval() method will run every second until it is told to stop. Inside the setInterval()method, we will run a function that runs every second.

What is the JavaScript equivalent of window addEventListener(load)?

The plain old JavaScript equivalent is: window.addEventListener ('load', function () { /* your code here */ }). does this appends the execution stack or replaces the old one? $.getScript ("my_lovely_script.js", function () { alert ("Script loaded and executed."); // here you can use anything you defined in the loaded script });


2 Answers

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 }) 
like image 158
Eruant Avatar answered Sep 20 '22 02:09

Eruant


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?

like image 25
Control Freak Avatar answered Sep 19 '22 02:09

Control Freak