How to make javascript variables global?
June 17, 2015 12:00 AM
Hi,
I try to integrate javascript arrays into lectora. I created some js variables and functions, attached the script as HTML-Extension type Header Scripting (sorry I have the German version of Lectora - not sure if I translate correctly). Everything works fine, as long as I stay on one page. Jumping between pages destroys the object(s). How can I store a javascript array/object for the whole project / aka making it a global variable? Seems I initialize the variable in the wrong place, or do not properly integrate the js-functions.
Any hints are greatly appreciated! I have attached my test project (Lectora 12) ...
Joachim Plener
undefined
Discussion (4)
Every Lectora's page is a separate HTML document. When navigating between pages, all scripts are loaded from scratch, all data is lost. Lectora uses Variable Manager to keep variables between pages. I never looked into how it works exactly, I just trust it to work. I assume it uses cookies (see attached screenshot).
So whenever I use JS arrays on a page, I serialize them and store in a Lectora variable before navigating away. Then when I need them on a different page, I read that variable and de-serialize the string into an array, using .split() and .join() respectively.

Exactly, don't store arrays, store strings. The easiest way is to simply do this:
//before leaving
var arr = [undefined
var arr = str.split(';,';); // [undefined]
Keep in mind that .join() will convert any numbers to strings, e.g.:
[1,2,3,4].join() --undefined1,2,3,4undefined1,2,3,4undefined1undefined2undefined3undefined4undefined1", 2, "4,3"]
var str = JSON.stringify(complex_arr) // ';[undefined1",2,"4,3"]';
var rebuild_array = JSON.parse(str) // undefined1", 2, "4,3"]
Hope this helps.
Thanks, Sergey for your fast reply! I had a feeling it could be that way and tried using a Lectora Variable for my array, by initializing it in Lectora and later redefining it as an array:
function initTriageArray(){if (Var_triageArray.getValue() == 0){
Var_triageArray = new Array();
}
}
I could use it, but only on one page, probably because Lectora variables can`t hold objects and therefore are reinitialized on a new page. As my Javascript knowledge is pretty limited (coming from 5+ years with Adobe Director) I thought I did something wrong with my implementation of the javasccript functions.
But now I know what to do: Convert the array to a string before leaving and rebuild it upon entering a page.
Quite ugly, but if it works - nevermind...
Thanks again, Joachim
undefined
Great! This does help indeed! I especially love that the JSON.stringifiy / parse functions can convert multidimensional arrays (with objects included) to a string and rebuild them. For example I have an array with many entries looking like that :
`[Before: Var_triageArray = new Array()]function createNewObject(name, wert, score){
pos = Var_triageArray.length;
Var_triageArray[pos] = new Object(); // new "Array" 3 Entries
Var_triageArray[pos]["Name"] = name; // String
Var_triageArray[pos]["Checked"] = wert; // 0 or 1
Var_triageArray[pos]["Score"] = score; // 1 to 5
}`
These JSON functions had no problem to stringify and parse again that kind of an array with objects and mixed values (string and integer) included.
As your post reached me, I was in the middle of writing my own parser with split and join and new... Now I need two lines to save and two lines to restore!
Thanks again, Joachim
Discussions have been disabled for this post