Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should you use a script loader?

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.

like image 734
Jethro Larson Avatar asked Jun 20 '11 18:06

Jethro Larson


People also ask

When should I load JavaScript?

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.

What does loader js do?

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!

Where should I load JavaScript?

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.

How do you load a script?

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.


1 Answers

Here's a good refection on script loaders

How, When, And Why Script Loaders Are Appropriate

like image 73
Jaime Avatar answered Sep 26 '22 02:09

Jaime