Skip to content

Files

Latest commit

author
jessekernaghan
Nov 21, 2015
5f14b9a · Nov 21, 2015

History

History
158 lines (105 loc) · 2.84 KB

README.md

File metadata and controls

158 lines (105 loc) · 2.84 KB

This project is no longer in use!


It was an effective tool and powered a lot of my projects well but I have since switched over to require.js which is an amazing implementation of AMD.


scriptr.js


This simple plugin hopes to improve performance by grabbing your scripts asynchronously, loading them conditionally, and ensuring that any script that they depend on load first.


Version

1.0

Features

  • full browser support, starting at IE8
  • callback on script load and fail
  • dependency support
  • conditional loading

Basic Implementation

var scriptloader = new scriptr();

//the first parameter (in this case, 'jquery') is an arbitrary slug for
//referencing this script when doing dependency loading
scriptloader.register('jquery', {
  url  : '/path/to/jquery.js'
});

scriptloader.loadscripts();

Multiple Scripts

var scriptloader = new scriptr();

scriptloader.register('script1', {
  url  : '/path/to/script1.js'
});

scriptloader.register('script2', {
  url  : '/path/to/script2.js'
});

scriptloader.register('script3', {
  url  : '/path/to/script3.js'
});

scriptloader.loadscripts();

Loading a script with a callback

var scriptloader = new scriptr();

scriptloader.register('jquery', {
  url    : '/path/to/jquery.js',

  onload : function(){
    alert('jQuery is ready to use!');
    jQuery(document).addClass('loaded');
  }

});

scriptloader.loadscripts();

Loading a script conditionally

var scriptloader = new scriptr();

scriptloader.register('html5shiv', {
  url    : '/path/to/html5shiv.js',

  check  : !document.createElement('canvas').getContext, //returns false in ie8, where html5shiv is needed

  onload : function(){
    alert('html5shiv activated!');
  }
});

scriptloader.loadscripts();

Loading a script with a dependency

var scriptloader = new scriptr();

scriptloader.register('jquery', {
  url    : '/path/to/jquery.js',
  onload : function(){
    alert('jQuery is ready to use!');
    jQuery(document).addClass('loaded');
  }
});

scriptloader.register('jqueryplugin', {
  url    : '/path/to/jqueryplugin.js',

  require : ['jquery'], //the slug of the dependency

  onload : function(){
    alert('jQuery plugin activated, but only after jQuery loaded!');
  }
});

scriptloader.loadscripts();

Deregistering Scripts

var scriptloader = new scriptr();

scriptloader.register('jquery', {
  url    : '/path/to/jquery.js',
  onload : function(){
    alert('jQuery is ready to use!');
    jQuery(document).addClass('loaded');
  }
});

scriptloader.deregister('jquery');

scriptloader.loadscripts(); //jquery won't be one of them