Rich-text Reply

Calling a Function Defined in Project Javascript

MJBeisch 05-11-15
Accepted Solution

Calling a Function Defined in Project Javascript

I'm testing out an idea that I had in regards to using the Project Javascript feature top help with experiment validation. I have a very simple function placed into the Project Javascript to set a cookie:

 

function EOScreateCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();

        date.setTime(date.getTime()+(days*24*60*60*1000));

        expires = "; expires="+date.toGMTString();
    }

    document.cookie = name+"="+value+expires+"; path=/";
}

 

I'm attempting to call that function from the browser's javascript console, so that I can temporarilly set a cookie that is used in an Audience condition. I've tried several methods for calling the function via console command, but get an undefined function error each time. Maybe I'm just having a brain-fart moment or spacing out on something obvious/simple, but how do I call a function declared in the Project Javascript from the console?

Matt Beischel - E-Commerce Optimization Specialist CohereOne

Level 2

tomfuertes 05-11-15
 

Re: Calling a Function Defined in Project Javascript

This works in theory. All project JS code is `eval`ed though so you have to export any of those functions specifically to the window so:

 

function foo () {/*stuff*/} 

 

should be 

 

window.foo = function () {/*stuff*/}

 

We have a series of boilerplate snippets on github for public viewing here! Poke around in ./project and ./experiments!

 

https://github.com/clearhead/optimizely-snippets

____
- Tom Fuertes | CTO @ CROmetrics / LinkedIn
"Most Impactful Use of Personalization" and "Experience of the Year" Optie award winner.


MJBeisch 05-11-15
 

Re: Calling a Function Defined in Project Javascript

That's exactly what it was. Needed to export the functions to the window level. Like I said, total brain-fart. Thanks Tom!

Matt Beischel - E-Commerce Optimization Specialist CohereOne

Level 2