Skip to Content


Dynamically load JavaScript resources after the page has already been rendered.

JavaScript July 2, 2019


nebula.loadJS(url, callback)


(Required) (String) The URL of the JS resource to load
Default: None

(Optional) (Function) A function to run after the JS resource has been loaded
Default: None

Request or provide clarification »

Additional Notes

Unlike jQuery getScript(), this function caches the JavaScript resource.

Source File

Located in /assets/js/nebula.js on line 3020.

Note: This function contains 1 to-do comment.

nebula.loadJS = function(url, callback){
    //@todo "Nebula" 0: listen for requestIdleCallback after IE11 no longer supported (still need conditional per Safari):
    if ( typeof url === 'string' ){
        var lazyScriptElement = document.createElement('script'); //Create a script element
        lazyScriptElement.src = url; //Set the script src to the URL

        //Callback when element has loaded
        if ( callback ){
            lazyScriptElement.onload = callback;

        //Track exceptions
        lazyScriptElement.onerror = function(){
            ga('send', 'exception', {'exDescription': '(JS) ' + url + ' could not be loaded', 'exFatal': false});
            nebula.crm('event', 'JavaScript resource could not be dynamically loaded');

        document.body.appendChild(lazyScriptElement); //Add the new script to the DOM
    } else {
        console.error('nebula.loadJS() requires a valid URL.');


To override or disable this JavaScript function, simply redeclare it with the exact same function name.

nebula.loadJS = function(url, callback){
    //Write your own code here, leave it blank, or return false.