From d283f7e661e14d6ae1881fe803e5b4f1ed0689ff Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 24 Jun 2024 13:49:08 -0400 Subject: Add 2024 Guix social talk. --- .../reveal.js/js/utils/loader.js | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2024-06-18-guix-social/reveal.js/js/utils/loader.js (limited to '2024-06-18-guix-social/reveal.js/js/utils/loader.js') diff --git a/2024-06-18-guix-social/reveal.js/js/utils/loader.js b/2024-06-18-guix-social/reveal.js/js/utils/loader.js new file mode 100644 index 0000000..58d39ac --- /dev/null +++ b/2024-06-18-guix-social/reveal.js/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 + const head = document.querySelector( 'head' ); + head.insertBefore( script, head.lastChild ); + +} \ No newline at end of file -- cgit v1.2.3