Coder Social home page Coder Social logo

microload's Introduction

Microload

Microload is a very small asynchronous script and stylesheet loader.

Demo

Here's microload: http://skid.github.io/microload.html

Usage

Simple as this:

microload('dependency_1.js', ['dependee_1.js', 'dependee_2.js', 'mystyles.css'], function(){
  console.log("Scripts Loaded");
});

The microload function accepts strings, arrays of strings and an optional function as the last argument.

Each string will be treated as a URL to a script or a stylesheet, depending on the extension. Each URL will be loaded consecutively. Microload won't attempt to load the second argument until the first argument is already loaded.

Microload also accpets arrays of URLs as arguments. These work just the same, except that all the URLs in the array will be loaded in parallel. In the next example we are loading jQuery and underscore in parallel (since they don't depend on other librares) and only after they are both loaded, we load our application scripts.

microload(['jquery.js', 'underscore.js'], ['application-1.js', 'application-2.js'], 'application-1-dependee.js');

Finally, if the last argument is a function, microload will execute it once all resources are succesfully loaded.

microload(['jquery.js', 'underscore.js'], 'application.js', 'application-dependee.js', function(){
  // Run something once everything is loaded
  application.init();
});

Microload will not load the same URL twice, but it will execute a callback queued after an already-loaded resource. For instance:

microload('application.js', function(){
  application.init();
  
  // Later ...
  microload('application.js', function(){
    // Oops. We load application.js again
    // This callback will execute immediately since application.js is already loaded.
  });
});

Microload will also queue callbacks for resources with the same path

microload('script-1.js', function(){
  // This will wait for script-1.js to load
});
microload('script-1.js', function(){
  // The script-1.js won't be loaded again, but this callback 
	// will execute once the previous load call returns.
});

Compatibility

Works with modern browsers (IE8+).

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.