Load a script from the given URL if it (window['attribute']) has not already been loaded.
window['attribute']
Usage:
loadScriptIfUndefined('xyz', 'https://example.com/lib/jquery.min.js') .then(() => { console.log('xyz is loaded.'); }) .catch(err => { console.log('Failed to load xyz.', err); }); Copy
loadScriptIfUndefined('xyz', 'https://example.com/lib/jquery.min.js') .then(() => { console.log('xyz is loaded.'); }) .catch(err => { console.log('Failed to load xyz.', err); });
Output:
xyz is loaded. Copy
xyz is loaded.
The name of the window attribute to check (e.g. jQuery, axios, etc.).
jQuery
axios
The URL of the script to load.
A Promise that resolves when the script has been loaded.
Load a script from the given URL if it (
window['attribute']
) has not already been loaded.Usage:
Output: