Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deferring JavaScript loading

Tags:

javascript

I have heard and read a few articles about deferring JavaScript loading and am very interested. It seems to be very promising for web apps that may be useful on Mobile platforms where the amount of JavaScript that can be loaded and executed is limited.

Unfortunately, most of the articles talk about this at an extremely high level. How would one approach this?

EDIT

Normally, all JavaScript is loaded on page load, however, there may be functions that are not necessary until a certain action occurs, at which time, the JavaScript should be loaded. This helps ease the burden of the browser on page load.

Specifically, I have a page that very heavily uses JavaScript. When I load the page on my phone, it won't load properly. As I debugged the page, I eliminated some of the JS functions. Once enough was eliminated, the page suddenly worked.

I want to be able to load the JS as needed. And possibly even eliminate the functions simply used for start up.

like image 275
steveo225 Avatar asked Sep 01 '11 14:09

steveo225


People also ask

What do we achieve by deferring the loading of JavaScript?

Deferring loading of JavaScript functions that are not called at startup reduces the initial download size, allowing other resources to be downloaded in parallel, and speeding up execution and rendering time.

How do you defer a script load?

Use async or defer # The async and defer attributes tell the browser that it may go on parsing the HTML while loading the script in the background, and then execute the script after it loads. This way, script downloads don't block DOM construction and page rendering.

What is deferring JS?

The defer is a Boolean value, used to indicate that script is executed after the document has been parsed. It works only with external scripts (i.e., works only when we are specifying the src attribute in <script> tag). It declares that the script will not create any content.

Should I use defer in JavaScript?

You should use defer for all other scripts. defer is great because it: Gets loaded as soon as possible — so it reduces load times. Doesn't execute until everything you need is ready — so all the DOM you need is there.


1 Answers

The basics are simple - breaking up your JavaScript code into logically separate components and loading only what you need. Depending on what you are building you can use:

Loaders:

  • Modernizr.load (or yepnope.js by itself)
  • LABjs
  • Many, many, many other deferred loading libraries.

Dependency managers (which are also loaders):

  • Require.js
  • dojo.require
  • JavaScript MVC's steal.js
  • Several other dependency management libraries.

These tools make use of a wide variety of techniques to defer the loading of scripts, the execution of scripts, manage dependencies, etc. What you need depends on what you are building.

You may also want to read through this discussion to learn something more about the pros and cons of using such techniques.


Response to edit:

There isn't really a good way to unload JavaScript that you have already loaded - the closest approximation you can get is to keep all of your loading code namespaced inside your application's namespace and then "clean up" by setting that namespace, and all references to it to null.

like image 88
Sean Vieira Avatar answered Sep 30 '22 14:09

Sean Vieira