In Web Development when working with external plugins (I'm struggling with one here), we often get the advise:
Ensure you load the scripts in the right order:
- Script 1
- Script 2
- Script 3
- ... etc
Is there an easy way a developer can confirm that his scripts are loaded in the correct order once the page has finished loading? Assuming one has the <script></script> tags added in the code in the right order.
I look at the chrome Timeline, but that is one confusing mess! I've no idea what is loaded first, what comes after it ... etc.
Any advise on this matter would greatly be appreciated.
Thank You.
-
Look at 'Network' tab of development tools, not 'Timeline'Anri– Anri2014年02月13日 08:45:57 +00:00Commented Feb 13, 2014 at 8:45
-
There are js libraries that can help you with this. For example RequireJS or the recently popular BrowserifyGohn67– Gohn672014年02月13日 08:46:01 +00:00Commented Feb 13, 2014 at 8:46
-
But the scripts should load in order if you use script tags. I think it is synchronous in that case.Gohn67– Gohn672014年02月13日 08:46:38 +00:00Commented Feb 13, 2014 at 8:46
-
what about 'view page source'?Pavlo– Pavlo2014年02月13日 08:47:14 +00:00Commented Feb 13, 2014 at 8:47
-
1Sure, as an example, use this link: jsfiddle.net/VTJBc . Go to Network tab ('Clear the data') and run the fiddle, you will see the order of included files at the end of the current tab.Pavlo– Pavlo2014年02月13日 09:23:18 +00:00Commented Feb 13, 2014 at 9:23
2 Answers 2
Generally, scripts are get loaded in the order they appear in code. Also you can use RequireJS library or similar to handle script dependencies.
If you are dealing with a messy multiple nested templates, or dynamic loading and can't be sure - you can use Network tab of Chrome DevTools.
Just filter it by Scripts. Lines appear in the same order they are processed.
3 Comments
JavaScript is synchronous until we set it to asynchronous by using async attibute:
<script async src="script.js">
External plug-in which are dependent on other libraries are advised to call after that library, e.g.:
<script type="text/javascript" src="script/jquery.min.js"></script>
<script type="text/javascript" src="script/plug-ins.js"></script>
Asynchronous scripts loading detail which can speed up your page are available at:
- https://developers.google.com/speed/docs/insights/UseAsync
- https://developers.google.com/speed/docs/insights/BlockingJS
Editors are welcome to add more information.