Rich-text Reply

$.getScript function not working

kamal_sahni 11-07-16

$.getScript function not working

I am trying to use $.getScript function in the variation, however it is showing me an error that $.getScript or window.optimizely.$.getScript is not a function. 

 

Any help??

JasonDahlin 11-07-16
 

Re: $.getScript function not working

Hi @kamal_sahni,

If you want to use $.getScript, a couple things to know:

1- Since it is not part of the jQuery you have loaded with Optimizely, you must reference it like this:

window.$.getScript()

2- You must delay executing that command until that plugin has loaded.  You may be able to use this function and wait for "$.getScript" to be not-undefined.  I've used this to update the value of variables and to wait for elements to be loaded via AJAX, but never for a library to be laoded... but it should work just the same.

/* _optimizely_evaluate=force */
function waitForDelayedContent(selector, experiment, timeout, keepAlive) {
    var intervalTime = 25;
    var timeout = timeout || 3000;
    var keepAlive = keepAlive || false;
    var maxAttempts = timeout / intervalTime;
    var attempts = 0;
    var interval = setInterval(function() {
        var selectorVal = (function(){
            try {
                return eval(selector);
            } catch(e) {
                return undefined;
            }
        })();
        if (typeof selectorVal !== 'undefined') {
            if (!keepAlive) {
                clearInterval(interval);
            }
            experiment();
        } else  if (attempts > maxAttempts) {
            clearInterval(interval);
        }
        attempts ++;
    }, intervalTime);
}

waitForDelayedContent("window.$.getScript", function(){
  // Whatever code you want to run or function you want to call once $.getScript is loaded
});



 

--Jason Dahlin
Analytics and Testing Guru Smiley Happy
kamal_sahni 11-14-16
 

Re: $.getScript function not working

Thanks @JasonDahlin

Your response looks useful. I will give it a try. Although, I fixed this issue by using the Javascript function that optimizely has mentioned here

 

Thanks for your help!