@sumedia/scriptloader
v1.2.2
Published
Utility to lazy-load scripts.
Downloads
13
Readme
ScriptLoader
Utility class to lazy-load external scripts, and/or to execute code only after the script finished loading.
Examples
Using anonymous callback
(new ScriptLoader('/myscript.js'))
.lazyLoad('.my-class', (element) => console.log(element));
Using predefined function
const logMyElement = (element) => console.log(element);
(new ScriptLoader('/myscript.js'))
.lazyLoad('.my-class', logMyElement);
Loading groups of scripts
Load six scripts divided in three groups. All scripts within a group are loaded asynchronously. The next group only starts loading when the previous group has finished.
(new ScriptGroupLoader())
.addGroup([
'/myscript.js',
'/yourscript.js',
])
.addGroup([
['/myscript.js'],
['/yourscript.js?onload=callbackName', 'callbackName'],
])
.addGroup([
{src: '/myscript.js', fnName: null},
{src: '/yourscript.js?onload=callbackName', fnName: 'callbackName'},
]);