If I load two scripts by html the second one will start running only after the first one has loaded or at the same time the first?
If I load two scripts by html the second one will start running only after the first one has loaded or at the same time the first?
Your question can have several interpretations. From what I understand you want to know if including 2 scripts tagged with script
, it will run one after another. In abstraction, the answer would be yes. Several factors can cause the second to run first. An example of this is if you put the async
attribute on the script
tag, there the browser that will manage which one runs first.
The scripts will run in the order in which they are loaded. Javascript by default is synchronous.
Note that this does not mean that all the logic of a script will be executed immediately when it is loaded. If you do something like this:
<script type="text/javascript">
$().ready(function () {
console.log("Script 1");
})
</script>
<script type="text/javascript">
console.log("Script 2");
</script>
<!---->
You can see in the console first "script 2", then "script 1". The first script actually ran first, but the part where it writes to the console is on hold until the DOM is ready.
Note that some people may say that Ajax
, setInterval
, setTimeout
etc. allow asynchronous programming. If you only work with these things you will still only use a single thread for your code. To run threads concurrently you will need Web Workers .