Rich-text Reply

document ready and window load

yifat 02-02-15
Accepted Solution

document ready and window load

Hi,

We would like to get the experimental variable in the document ready rather then the window load as we have dynamic web pages app. Is it posible to get it as soon as the DOM is loading and not after all images are loaded?

 

Currently we issue a function with the variable inside it in the experiment javascript. then we call it from the edit code in the dashboard and we see we can approach it only from the window.load and not fron the document.ready. In the document ready this value is undefined and only in the window/load he has a value. 

 

Thanks in advanced. 

Level 1

Brian_Abad 02-02-15
 

Re: document ready and window load

Thanks for your question!

If the code utilized within the < edit code > box of your experiment is pure JavaScript then Optimizely will wait until DOM ready to execute the code. Any code present beneath pure JavaScript will also wait until DOM ready to execute. More on this concept is covered in our knowledge base article: https://help.optimizely.com/hc/en-us/articles/200040335-How-Optimizely-Works-Snippet-order-of-execut...

 

If you'd like to execute pure JavaScript before DOM ready, you could wrap that code within Optimizely's evaluate markup which looks like:

 

/* _optimizely_evaluate=force */
   your "pure JS" goes here
/* _optimizely_evaluate=safe */

 Does this answer your question? Let me know if any of this is unclear of if I've misunderstood your question.

---
Brian Abad
Manager, Technical Support
Customer Success
Optimizely
yifat 02-08-15
 

Re: document ready and window load

Yes. It was very helpful. this was exactly what I needed. 

 

BTW - it won't work with jquery code inside the <edit code>?

 

 

Thank you very much. 

Level 1
Brian_Abad 02-09-15
 

Re: document ready and window load

jQuery inside the < edit code > box will run as the page loads as long as there is no pure JavaScript that is present before it. If there is pure JavaScript present within your code, that line of code as well as any code beneath it will wait until DOM ready to execute.

Let me know if I've misunderstood your question!
---
Brian Abad
Manager, Technical Support
Customer Success
Optimizely

Re: document ready and window load

So if javascript code in the <edit code> box runs at DOM ready by default, and we have a script containing code that will also run on DOM ready, can we be sure that Optimizely's code will run first? For example, if I have this in the  <edit code> box:

myVariable = true;

 

and I have this code in one of our own scripts:

// False by default
var myVariable = false;

$(document).ready(function(){
   if (myVariable) {
      //do stuff
   }
});

Will we ever do stuff? In other words, does Optimizely set myVariable to true after it is initialized to false but before our DOM ready code executes?