Accessing variables from javascript

slaubach8824 Community Member Posts: 5
Hello Friends,

I don't mean to bother you, but since i read a bunch of discussings now, and still was not able to solve the problem i need to ask:

I'd like to write the name of the user by using "AICC_Student_Name"... that works fine so far, unless it is formatted "lastname,firstname".

Therefore I found the Java script that splits the names and seems to be written perfectly in JAVA Code.


It doesn't work ...


So i tried and tried and lately found out, that the main problem is, that JavaScript does not get access to the variables in lectora.

Just to figure out the Problem i first checked Java is working... "alert" works! Fine so far!
then i tried getting and setting variable values

The .set method works great but a in-lectora defined variable named "_test" containing "Mustermann,Max" is not accessable!



would not work, although



i have no clue!

Do you?


  • timk
    timk Community Member Posts: 1,205 ♦ Idol ♦
    To get access to a Lectora variable in javascript you have to do something with it in Lectora. Otherwise Lectora won't create that variable. You can use an action to modify the variable in Lectora, e.g. on page level:

    On: Show
    Action: Modify variable
    Target: _test
    Type: Set equal to
    Value: VAR(AICC_Student_Name)

    Mind that AICC_Student_Name is only accessible after publishing to Scorm and uploading to your LMS.

    Then you can use it in javascript, e.g.

    alert(Var_test.getValue()); (Make sure it's "getValue" not "GetValue")

  • slaubach8824
    slaubach8824 Community Member Posts: 5
    Hey  Tim, thanks for your support!

    One important hint was to use variables "somehow" inside Lectora first. But it still did not work!

    Anyway, I was able to isolate the core of the problem and would like to give sound advice to all users with similar problems:

    First of all:

    Tim K's Java-Code works perfectly!

    Just read
    and try!

    If there is still trouble, just change the line

    "var username = VarAICC_Student_Name.getValue().split(“, “);"


    "var username = VarAICC_Student_Name.getValue().split(“,“,2);"

    After adding the parameter it finally worked!
    Sometimes (i think it is depending on the Java-Version) the Java split-method necessarily needs the parameter "2" to "know" in how many pieces it shall split the string!

    Thanks for all your support and feel free to contact if you have any further question!
  • klaatu
    klaatu Community Member Posts: 988
    FYI - Java and JavaScript are two completely different animals. You are referring to JavaScript.
  • timk
    timk Community Member Posts: 1,205 ♦ Idol ♦
    I've just reread my code and there's indeed a small mistake in it, which probably has been made by that old forum software that used to add blanks in random places. As AICC_Student_Name contains the users name in the form "Surname,Firstname" it must be .split(",") (no blank) instead of split(", "). The "limit" parameter is optional and it doesn't tell the number of substrings that shall be created, it tells how many of the substrings shall be added to the new variable:

    The code 'var username = VarAICC_Student_Name.getValue().split(“,“,1);' would create two substrings "Surname" and "Firstname" but "username" would only contain "Surname".

  • slaubach8824
    slaubach8824 Community Member Posts: 5
    @ Tim we got that going!
    What do you (as the professional) think about reposting it in a total complete thread with all the advice we brought together now?
    That the parameter was optional was just an educated guess from me ... ;o)

    Again.... Thank you very very much for your effort!


    ....well, if you say so...
    Unfortunately you did not give a short description of how to distinguish Java and Javascript from each other!
    Just to make me clever, i would appreciate that!
  • klaatu
    klaatu Community Member Posts: 988
    Here's the two minute version.

    Java is an object oriented programming language that is authored in an integrated development environment (IDE), and then compiled into bytecode. The bytecode is then run in a java virtual machine (JVM). The Java Virtual Machine must be installed on the end users computer (or any other computing device) in order to be able to run Java programs. Once the JVM is installed, Java programs or "applets", can be run in a browser or as a stand alone application. "Applets" means "small applications". The Java language library is much larger than JavaScript and is not easily readable to humans. A lot of programmers start with Java.

    JavaScript, on the other hand, is a scripting language that can be authored in any text editor. It is then interpreted at run time by internet browsers without the need to compile it into bytecode. JavaScript is embedded in HTML and run in a browser, it usually does not stand alone like Java. While it too is object oriented, it is much smaller and easily read and understood by humans.

    JavaScripts main function is to provide interactivity in web sites that HTML alone cannot accomplish. When used in a web site, a Java applet is a separate program independent of the HTML although it is called to run by it. As advertised by Oracle, Java is the language of over 3 billion devices like Blu-ray players, printers, ATM Machines, car navigation systems, the list goes on and on.

    A long, long time ago (in this galaxy), the two crossed paths but went in very different directions. JavaScript was known by many different names with the first standard called ECMAScript then finally ending up JavaScript. Many believe it was intentionally named this way to make people believe it was similar to the Java language even though it is quite different.

    There is a lot of information on the web about the similarities and differences of the two if you want more info.

  • timk
    timk Community Member Posts: 1,205 ♦ Idol ♦
    I don't think there's need for a new thread, as it's all in there already: