l.js: Load CSS and JavaScript files in parallel

Recommend this page to a friend!
  Info   View files View files (8)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-10-28 (-1 hours ago) RSS 2.0 feedNot enough user ratingsTotal: 267 All time: 171 This week: 4Up
Version Licenses JavaScript version Categories
ljs 1.0GNU General Publi...1.0HTML, jQuery
Description 

Author

This object can be used to load CSS and JavaScript files in parallel.

It can queue given CSS and JavaScript files to be loaded at the same time the rest of the page is loaded. It keeps track of loaded files to avoid loading each of them more than once.

A callback function may be called when each CSS and JavaScript file is loaded.

Picture of Jonathan Gotti
  Performance   Level  
Name: Jonathan Gotti <contact>
Classes: 8 packages by
Country: France France
Age: 47
All time rank: 262 in France France
Week rank: 6 Up2 in France France Equal
Innovation award
Innovation award
Nominee: 5x

Winner: 3x

Details
# l.js is another simple/tiny javascript/css loader [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmalko%2Fl.js.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmalko%2Fl.js?ref=badge_shield) ## features - compatible with all ECMASCRIPT 5.1 browsers (Warning for old users this is a breaking change on supporting long time deprecated browsers) - parallel script / css loading - callback after script loading (css support callback too but are executed imediately) - tiny only 2ko uglifyed, less than 1.1ko gziped (at least for latest revision) - may load in order to preserve dependencies - support aliases for simpler calling - on demand loading - only one script tag required - clear syntax (to use not in the source code :) ) - successive load of same file will load it once but execute all callbacks associated - can dumbly check already inserted tags at load time - may use a fallback url on error (only for js files and with error events compatible browsers) - may register error handlers (only for js files and with error events compatible browsers) - can load javascripts modules ## examples ### loading ljs and another libs with one single tag ```html <script src="l.js"> ljs.load('myLib.js',function(){ /* your callback here */}); </script> ``` or using jsdeliver CDN: ```html <script src="https://cdn.jsdelivr.net/gh/malko/l.js@latest/l.min.js"> ljs.load('myLib.js',function(){ /* your callback here */}); </script> ``` > If you're looking for the npm package, you should search for **@malko/l.js** > ```npm install @malko/l.js``` ### loading some scripts in parallel others in order ```html <script src="l.js"> ljs .load('myLib.js') .load('myRequiredLib.js','myDependentLib.js',function(){ /* your callback here */}) ; </script> ``` second load will be executed in parallel of first load but myDependentLib.js won't load before myRequireLib.js is loaded ```html <script src="l.js"> ljs.load(['myLib.js','myRequiredLib.js'],'myDependentLib.js',function(){ /* your callback here */}); </script> ``` this will load myLib.js and myRequiredLib.js in parrallel and wait for them before loading myDependentLib.js ### using some aliases for simpler loading ```html <script src="l.js?checkLoaded"> // <- adding checkLoaded to the url will dumbly check already inserted script/link tags ljs .addAliases({ jQuery:'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js#jqueryId' // <- script tag will have attribute id=jqueryId ,ui:[ 'jQuery' ,'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js' ,'myUITheme.css' ] }) .load('ui',function(){ /* work with both jquery and jquery-ui here */ }) ; </script> ``` ### define a fallback url l.js also support a fallback url for javascript files in case you want to try to get the resource from another location on loading failure You can define this fallback url parameter like you define ids. The difference is you will prefix with **#=** instead of # alone ```html <script src="l.js"> ljs.load('http://domain.com/myLib.js#=/myfallback.js#myid',function(){ /* generated script tag will have myid as id and will try to load /myfallback.js if it fail to load http://domain.com/myLib.js */ }); </script> ``` ### register an error handler ```html <script src="l.js"> ljs .load('missingFile.js',function(){ /* your callback here */}) .onError(function(url) { console.log('error loading', url); // <- will print "error loading missingFile.js" }) ; </script> ``` ### load a js module As a default l.js load js scripts with their type attribute set to `text/javascript`. To set type to `module` instead, simply prefix the url with `module:` ```html <script src="l.js"> ljs.load('module:myModuleLib.js',function(){ /* your callback here */}); </script> ``` this piece of code is dual licensed under MIT / GPL Hope this help, code review, suggestions, bug reports are welcome and appreciated. ## License [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmalko%2Fl.js.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmalko%2Fl.js?ref=badge_large)
  Files folder image Files  
File Role Description
Files folder image.github (1 file)
Accessible without login Plain text file bower.json Data Auxiliary data
Plain text file l.js Class source code
Plain text file l.min.js Class minified code
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file pnpm-lock.yaml Data Auxiliary data
Accessible without login Plain text file README.md Doc. documentation

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:267
This week:0
All time:171
This week:4Up