e.g. lab.js, yepnope.js, require.js
I'm a little fuzzy on at what point it's advantageous to use a script loader. I know that when you have significant browser, or feature targeted js, it can be good. But there's performance tradeoffs for making extra requests versus just concatenating scripts together, both in terms of UI responsiveness and page load time. And then there's optimizing for caching to worry about as well.
I have a feeling I'm going to get a lot of "It depends" answers.
Simply put, if your script have snippets that would start executing right away (outside all function(){} bodies) and that access DOM elements, it should go at the end of the page so that DOM would have been built and be ready to be accessed by the time the script starts executing.
Loaders can transform files from a different language (like TypeScript) to JavaScript or load inline images as data URLs. Loaders even allow you to do things like import CSS files directly from your JavaScript modules!
The <script> tag can be placed in the <head> section of your HTML or in the <body> section, depending on when you want the JavaScript to load. Generally, JavaScript code can go inside of the document <head> section in order to keep them contained and out of the main content of your HTML document.
For loading a script file dynamically using JavaScript, the basic steps are: Create the script element. Set the src attribute on the script element to point to the file we want to load. Add the script element to the DOM.
Here's a good refection on script loaders
How, When, And Why Script Loaders Are Appropriate
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With