How to make javascript variables global?

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
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
-
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. -
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(){
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.
if (Var_triageArray.getValue() == 0){
Var_triageArray = new Array();
}
}
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
-
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. -
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()]
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.
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
}`
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
Categories
- 35.8K All Categories
- 109 ✫ Announcements
- 33.1K Lectora®
- 31.1K Lectora Discussions
- 28.9K Lectora Desktop
- 2K Lectora Online
- 2K Lectora Feature Requests
- 71 Lectora User Groups
- 36 Lectora Accessibility User Group (LAUG)
- 22 ELB Learning Content
- 22 ELB Learning Content Discussions
- 321 CenarioVR®
- 188 CenarioVR Discussions
- 133 CenarioVR Feature Requests
- 41 Rockstar Learning Platform®
- 38 Rockstar Learning Platform Discussions
- 108 CourseMill®
- 108 CourseMill Discussions
- 47 ReviewLink®
- 47 ReviewLink Discussions
- 5 The Training Arcade®
- 5 The Training Arcade Discussions
- 934 All Things eLearning
- 36 eLearning Development
- 546 Learning Management System (LMS) Integration
- 332 Web Accessibility
- 1.2K ♪ The Green Room
- 9 Additional Learning Products