0

What is the execution flow of the external hosted javascript files with respect to the window.onload invokation. Say we have below code in the external file:

(function() {

   console.log('logging1...');
})();

var func1 = function() {
   console.log('func1 invoked!');
};

func1();

Before the window.load gets fired,

1.) What is the execution flow or output of the above code?

2.) Is there a possibility for any code of the file getting executed after window.load?

3.) Parallel with window.load, can an external file code be executed.

Madav
  • 311
  • 1
  • 5
  • 12
  • This question is full of ambiguity for me. I suggest reading through http://stackoverflow.com/questions/8996852/load-and-execute-order-of-scripts and https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload and asking a more specific question. Also, javascript is not multithreaded. You cannot run anything in parallel in js, (although you can use timeouts) – Daniel Moses Jun 15 '16 at 18:21

1 Answers1

0

1.) What is the execution flow or output of the above code?

Usually it is:

logging1...
func1 invoked!

There is no ambiguity in the order of function invocations here. But pay attention that console.log() is not standardized to immediately print the output. It could very well return synchronously without printing the given arguments in the order of console.log() function invocations.

2.) Is there a possibility for any code of the file getting executed after window.load?

No (apart from code in event listeners, timers and the like, of course).

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading. (https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onload)

3.) Parallel with window.load, can an external file code be executed.

What do you mean with parallel? JavaScript is single threaded.

le_m
  • 19,302
  • 9
  • 64
  • 74