another js loader
<script src="l.js">
ljs.load('myLib.js',function(){ /* your callback here */});
</script>
or using jsdeliver CDN:
<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
<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
<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
<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>
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
<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>
<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>
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:
<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.