How to make javascript variables global?

jplener
jplener Community Member Posts: 5
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

 

Comments

  • ssneg
    ssneg Community Member Posts: 1,466 ☆ Roadie ☆
    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.
  • jplener
    jplener Community Member Posts: 5
    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

     
  • ssneg
    ssneg Community Member Posts: 1,466 ☆ Roadie ☆
    Exactly, don't store arrays, store strings. The easiest way is to simply do this:
    //before leaving
    var arr = ["one","two","five"]
    var str = arr.join(); // "one,two,five"
    VarMyLectoraVariable.set(str);
    
    //after loading new page
    var str = VarMyLectoraVariable.getValue();
    var arr = str.split(','); // ["one","two","five"]

    Keep in mind that .join() will convert any numbers to strings, e.g.:
    [1,2,3,4].join() --> "1,2,3,4"
    "1,2,3,4".split(",") --> ["1", "2", "3", "4"]

    Also, if any value in your array contains a comma, it will mess things up badly:
    ["Anna","Adams, John","Smith, Bob"].join() --> "Anna,Adams, John,Smith, Bob"
    "Anna,Adams, John,Smith, Bob".split(",") --> ["Anna", "Adams", " John", "Smith", " Bob"] (oops!)

    So if you want a robust way of serializing and de-serializing arrays for storing them as strings, use JSON.stringify() and JSON.parse() functions. It works like this:
    var complex_arr = ["Anna","Adams, John","Smith, Bob", "1", 2, "4,3"]
    var str = JSON.stringify(complex_arr) // '["Anna","Adams, John","Smith, Bob","1",2,"4,3"]'
    var rebuild_array = JSON.parse(str) // "Anna", "Adams, John", "Smith, Bob", "1", 2, "4,3"]

    Hope this helps.
  • jplener
    jplener Community Member Posts: 5
    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