Dejvino
d3771ec49d
Last commit: commit e1c180565e48ae1b595f44edef319872262a99c7 Merge: b8d97d2 f2b0316 Author: Hakim El Hattab <hakim.elhattab@gmail.com> Date: Wed Aug 9 15:17:25 2023 +0200 Merge pull request #3445 from t-fritsch/fix-scss-watch-tasks fix scss watch tasks broken on syntax error
46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
/**
|
|
* 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 );
|
|
|
|
} |