Rich-text Reply
Highlighted

write javascript function in code editor

levitt 03-12-19

write javascript function in code editor

I intend to add this [ accordion ] javascript function to the variation via the code editor, 

 

I managed to load the script as the following way, 

 

var $ = window.$;

$('head').append($('<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>',
'<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>'));

 

then I was trying to call the function like this, 

$(function() {
$( "#accordion" ).accordion({
collapsible: true
});
});

I got the error message that says "accordion is not a function". 

 

i need some help here on how should I write the function correctly, thank you very much in advance Smiley Happy 

Level 2

CouchPsycho 03-12-19
 

Re: write javascript function in code editor

Ok,

 

do you get the same message when previewing the variation?

 

I see two possible problems:

 

  1. Unprobable: The editor is not loading the script or doing it there is not access to the function from inside the editor.
  2. Probable: A timing issue, your editor code is executed while the jQuery or jQueryUI is not loaded/ready.

Where do you add the libraries? Experiment Code? Variation Code? 

 

Try to add somtehing like

 

if (typeof jQuery === 'undefined') 
{
  alert("There is no jQuery available here...");
} 

 

in your variation code, to check availability of jQuery first to get an idea.

Kind regards

Michael


   
JasonDahlin 03-12-19
 

Re: write javascript function in code editor

Hi @levitt ,

 

I agree with @CouchPsycho - it's probably a timing issue.

Your experiment will need to wait for "typeof $.ui.accordion" to be a function before you can use it.

 

--Jason Dahlin
Analytics and Testing Guru Smiley Happy


Experimentation Hero
levitt 03-13-19
 

Re: write javascript function in code editor

hi Michael, 

 

when i preview the variation, i didn't get any error message, just the function is not working, I inspected the page, i could see that the two scripts are loaded.

 

i put these two library in the variation code and together with function. 

 

I got this error message when I click on "save and apply"

Level 2
JasonDahlin 03-13-19
 

Re: write javascript function in code editor

If you didn't use "window.alert" it may have thrown an error trying to run the code.
When debugging stuff, always put in affirmative and negative messages so that you know it ran.


e.g.

if (thing) { window.alert('thing exists') } else { window.alert('no thing') }

That way, you know for sure it ran and what the result of your test condition is.

Side note: Preview Mode does not use the exact same timing as running the experiment for real (it's a minor difference, but one that I had an issue with when dealing with a race condition). My suggestion is to create a "Test User" audience that you can attach to the experiment and make it something that you can do that no real user would do, like test for the URL parameter "levitt=here".

--Jason Dahlin
Analytics and Testing Guru Smiley Happy


Experimentation Hero