Rich-text Reply

Test working when code pasted in Experiment JS but doesnt work when pasted in Variation JS

inigam 08-08-16
Accepted Solution

Test working when code pasted in Experiment JS but doesnt work when pasted in Variation JS

Hi

 

I am using below code

intended to change a lightbox which appears slowly in page load.

 

 

$(document).ready(function() {
    var now = new Date();
    newbox();

//do something make changes using jQuery
    console.log(window.performance.timing.domContentLoadedEventStart-now); // just to know more about

});


 

This doesn't work when pasted in Variation Code and 'do somthing ie changes' do not occur,  but works when pasted in Experiment JS and changes happen. problem is then Original also changes.

 

Thanks for inputs in advance.

 

 

Regards,
IN


Level 6

robertchan 08-08-16
 

Re: Test working when code pasted in Experiment JS but doesnt work when pasted in Variation JS

This is likely a scoping/race issue. You need to make sure the variables you're using when you "do something make changes using jQuery" are available at that point of execution. You can attempt to resolve either issue through one or a combination of the following ways:

 

- For timing, if you need your variation code to evaluate immediately, you can surround your code with the following and check whether document ready is even necessary:

/* _optimizely_evaluate=force */
   //YOUR CODE HERE
/* _optimizely_evaluate=safe */

- For scoping, a quick fix is to use window to declare variables/functions that will be used globally.

//For example, instead of
var somevariable = document.getElementsByClassName('blah')[0];

//you could use
window.somevariable = document.getElementsByClassName('blah')[0];
//now the variable is accessible globally
Robert Chan

Experimentation Hero
inigam 08-09-16
 

Re: Test working when code pasted in Experiment JS but doesnt work when pasted in Variation JS

Thanks Robert,

the second one worked, i declared functions globally instead.
Regards,
IN


Level 6
robertchan 08-09-16
 

Re: Test working when code pasted in Experiment JS but doesnt work when pasted in Variation JS

Anytime, and great to hear it worked!
Robert Chan

Experimentation Hero