Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

loadjs

Package Overview
Dependencies
0
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    loadjs

Tiny async loader for modern browsers


Version published
Maintainers
1
Created

Changelog

Source

1.0.2 - May 18, 2016

  • Added bower.json
  • Removed onload script deletion

Readme

Source

LoadJS

LoadJS is a tiny async loader for modern browsers (506 bytes).

Dependency Status devDependency Status

Introduction

LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code in your <html> and then use the loadjs global to manage JavaScript dependencies after pageload.

LoadJS is based on the excellent $script library by Dustin Diaz. We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/failure callbacks and to optimize the library for modern browsers. LoadJS is 506 bytes (minified + gzipped).

Here's an example of what you can do with LoadJS:

// define a dependency bundle
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');

// execute code elsewhere when the bundle has loaded
loadjs.ready('foobar', function() {
  // foo.js & bar.js loaded
});

The latest version of LoadJS can be found in the dist/ directory in this repository:

You can also use it as a CJS or AMD module:

$ npm install --save-dev loadjs
var loadjs = require('loadjs');

loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');

loadjs.ready('foobar', function() {
  // foo.js & bar.js loaded
});

Browser Support

  • IE9+
  • Opera 12+
  • Safari 5+
  • Chrome
  • Firefox
  • iOS 6+
  • Android 4.4+

Documentation

// load a single file
loadjs('/path/to/foo.js', function() {
  // foo.js loaded
});


// load multiple files (in parallel)
loadjs(['/path/to/foo.js', '/path/to/bar.js'], function() {
  // foo.js & bar.js loaded
});


// load multiple files (in series)
loadjs('/path/to/foo.js', function() {
  loadjs('/path/to/bar.js', function() {
    // foo.js loaded then bar.js loaded
  });
});


// add a bundle id
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar', function() {
  // foo.js & bar.js loaded
});


// add a failure callback
loadjs(['/path/to/foo.js', '/path/to/bar.js'],
       'foobar',
       function() { /* foo.js & bar.js loaded */ },
       function(pathsNotFound) { /* at least one path didn't load */ });


// execute a callback after bundle finishes loading
loadjs(['/path/to/foo.js', '/path/to/bar.js'], 'foobar');

loadjs.ready('foobar', function() {
  // foo.js & bar.js loaded
});


// .ready() can be chained together
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');

loadjs
  .ready('foo', function() {
    // foo.js loaded
  })
  .ready('bar', function() {
    // bar.js loaded
  });


// compose more complex dependency lists
loadjs('/path/to/foo.js', 'foo');
loadjs('/path/to/bar.js', 'bar');
loadjs(['/path/to/thunkor.js', '/path/to/thunky.js'], 'thunk');


// wait for multiple depdendencies
loadjs.ready(['foo', 'bar', 'thunk'],
             function() {
               // foo.js & bar.js & thunkor.js & thunky.js loaded
             },
             function(depsNotFound) {
               if (depsNotFound.indexOf('foo') > -1) {};  // foo failed
               if (depsNotFound.indexOf('bar') > -1) {};  // bar failed
               if (depsNotFound.indexOf('thunk') > -1) {};  // thunk failed
             });


// use .done() for more control
loadjs.ready('my-awesome-plugin', function() {
  myAwesomePlugin();
});

loadjs.ready('jquery', function() {
  // plugin requires jquery
  window.myAwesomePlugin = function() {
    if (!window.jQuery) throw "jQuery is missing!";
  };

  // plugin is done loading
  loadjs.done('my-awesome-plugin');
});

Directory structure

loadjs/
├── dist
│   ├── loadjs.js
│   ├── loadjs.min.js
│   └── loadjs.umd.js
├── examples
├── gulpfile.js
├── LICENSE.txt
├── package.json
├── README.md
├── src
│   └── loadjs.js
├── test
└── umd-templates

Development Quickstart

  1. Install dependencies
  1. Clone repository
$ git clone git@github.com:muicss/loadjs.git
$ cd loadjs
  1. Install node dependencies using npm
$ npm install
  1. Build examples
$ ./node_modules/.bin/gulp examples:build

To view the examples you can use any static file server. To use the nodejs http-server module:

$ npm install http-server
$ ./node_modules/.bin/http-server -p 3000

Then visit http://localhost:3000/examples

  1. Build distribution files
$ ./node_modules/.bin/gulp dist:build

The files will be located in the dist directory.

Run tests

To run the browser tests first build the loadjs library:

$ ./node_modules/.bin/gulp test:build

Then visit http://localhost:3000/test

Keywords

FAQs

Last updated on 18 May 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc