Function loadScriptIfUndefined

  • Load a script from the given URL if it (window['attribute']) has not already been loaded.

    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);
    });

    Output:

    xyz is loaded.
    

    Parameters

    • windowAttribute: string

      The name of the window attribute to check (e.g. jQuery, axios, etc.).

    • url: string

      The URL of the script to load.

    Returns LoadScriptReturns

    A Promise that resolves when the script has been loaded.