diff options
author | Marvin Borner | 2021-07-13 19:40:17 +0200 |
---|---|---|
committer | Marvin Borner | 2021-07-13 19:40:17 +0200 |
commit | 08cd951ea8410bc87e316e6c11d34a118437ba8b (patch) | |
tree | f57baaee689ee81b590bb5d01df3591da10cccc7 /presentation/js/utils/loader.js | |
parent | 1b429c0fc28e5cd8b474ad5a1de1fa6f3d7c2e2a (diff) |
Added colloq summary and final presentation
Diffstat (limited to 'presentation/js/utils/loader.js')
-rw-r--r-- | presentation/js/utils/loader.js | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/presentation/js/utils/loader.js b/presentation/js/utils/loader.js new file mode 100644 index 0000000..58d39ac --- /dev/null +++ b/presentation/js/utils/loader.js @@ -0,0 +1,46 @@ +/** + * Loads a JavaScript file from the given URL and executes it. + * + * @param {string} url Address of the .js file to load + * @param {function} callback Method to invoke when the script + * has loaded and executed + */ +export const loadScript = ( url, callback ) => { + + const script = document.createElement( 'script' ); + script.type = 'text/javascript'; + script.async = false; + script.defer = false; + script.src = url; + + if( typeof callback === 'function' ) { + + // Success callback + script.onload = script.onreadystatechange = event => { + if( event.type === 'load' || /loaded|complete/.test( script.readyState ) ) { + + // Kill event listeners + script.onload = script.onreadystatechange = script.onerror = null; + + callback(); + + } + }; + + // Error callback + script.onerror = err => { + + // Kill event listeners + script.onload = script.onreadystatechange = script.onerror = null; + + callback( new Error( 'Failed loading script: ' + script.src + '\n' + err ) ); + + }; + + } + + // Append the script at the end of <head> + const head = document.querySelector( 'head' ); + head.insertBefore( script, head.lastChild ); + +}
\ No newline at end of file |