Explore the community Forums Lectora Lectora Questions & Answers Lectora variable undefined in JavaScript unless referenced in Lectora first?

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #348349 Score: 0
    Profile photo of Andrew Corbett
    Andrew Corbett
    Member
    intermediate
    wise owl
    advanced
    curious george
    picture perfect
    contributor
    1 pt
    @acorbett3701

    I’m having an odd problem with Lectora variables and referencing them within JavaScript…

    I have a var defined in the Variable Manager that I access and change through JavaScript using an OnShowRunJS action. However, it appears that unless I’ve referenced the variable via a Lectora action prior to calling this OnShowRunJS action, then VarMyVariable is undefined (when I try to reference it in JS). I would not have expected to need to do this.

    All well and good except that now MyVariable is being saved between sessions (to Session Storage under “Lectora2:mytitlename”) even though I have not turned on the save between sessions property.  This is not good as I want it to be initialized newly at the start of each session.

    Does anyone have insights into either or both of these problems? Point me to documentation or to another post if easiest.

    Thanks!

    andrew

    • This topic was modified 2 months, 2 weeks ago by Profile photo of Andrew Corbett Andrew Corbett. Reason: clarify question
    #348373 Score: 0
    Profile photo of Andrew Corbett
    Andrew Corbett
    Member
    intermediate
    wise owl
    advanced
    curious george
    picture perfect
    contributor
    1 pt
    @acorbett3701

    Ooops, take away my “Advanced” badge!  They are supposed to be in Session Storage to access between pages.  So ignore that part of my question …. it is not a problem.

     

    #348379 Score: 1
    Profile photo of Tim K
    Tim K
    Member
    contributor
    beginner
    intermediate
    advanced
    friend finder
    junior moderator
    advocate
    wise owl
    curious george
    Group Member
    181 pts
    @timk

    It has always been like that: if you don’t use a (user-defined) variable in a Lectora action it’s not initialized. The easiest way to use all variables is by adding an action group on title level with only one action: Reset all variables

    This way all variables are initialized on all pages and can be adressed in javascript.

    It’s new to me that variables used in any way are saved between sessions independent of the setting in the variable manager… Mind, the browser saves values by itself.

    Tim

    This post has received 1 vote up.
    #348381 Score: 0
    Profile photo of Andrew Corbett
    Andrew Corbett
    Member
    intermediate
    wise owl
    advanced
    curious george
    picture perfect
    contributor
    1 pt
    @acorbett3701

    Thank you Tim!  I see that action and now understand what it is for.

    (Re other issue, see my update above.  I did seem to be having an odd problem with variables unintentionally saving across sessions, but Session Storage is clearly not involved, and I’m still trying to reproduce).

    thx,

    andrew

    #348405 Score: 0
    Profile photo of Andrew Corbett
    Andrew Corbett
    Member
    intermediate
    wise owl
    advanced
    curious george
    picture perfect
    contributor
    1 pt
    @acorbett3701

    Some additional info here for those who may stumble on this question later:

    1. You do indeed need to reference a Lectora variable through an action within Lectora before it is accessible to your custom JavaScript using the VarMyVariable.set() and .getValue() functions.
    2. This makes sense as Lectora needs to declare those variables in it’s JavaScript code before they become visible to custom JS code.  And those declarations are only made if there are references to the variable in authoring tool on that screen.
    3. I have found the following trick works for me to make sure the variables I want in custom JS are available:
      1. I create an OnShowModVar action on each separate screen of the title
      2. I include every variable I want to have access to in the details of the “Value” field for the ModVar action
      3. Notice that this is simply using these variables as inputs and not changing their values
      4. If I put this action in front of all other actions on the screen, then the variables are defined when I want to use them in custom JS calls
    4. I have NOT been successful in using the “Reset All Variables” action. When I use this function the variables ARE defined, but they also appear to truly be reset to their initial values (as specified in the Variable Manager).  In my case, I want them simply to have the same value as they had on exiting whatever the previous screen was.
    5. As I noted above, Lectora variables are stored using the JavaScript Session Storage mechanism and are visible in the JS developers tools in various browsers (or at least in Chrome).  This makes sense as this is the intended use of Session Storage in JavaScript — whatever is stored there lives throughout the current session under the current browser window, but disappears when the window is closed. There is also a Local Storage mechanism for saving variables across sessions, which I have used in custom code to save state.
    6. Lectora variables are NOT being unintentionally saved between sessions as I had suggested above.  That was a mistaken observation on my part resulting from odd behavior which I can’t reproduce (thankfully).

    andrew

    #348453 Score: 0
    Profile photo of Math Notermans
    Math Notermans
    Member
    contributor
    intermediate
    advanced
    friend finder
    junior moderator
    advocate
    LUC16 Attendee
    LUC16
    wise owl
    curious george
    57 pts
    @mnotermans5114

    Interesting topic 🙂 Especially Tim’s trick on using ‘Reset All Variables’ to ensure all custom variables in your project are defined is nice i think. Gonna try that one next time… didnot think of it before.

    Wondering though why Andrew is not succesfull in using this as he states on point 4…. ?

    If you add a ‘Reset All Variables’ on your first screen, then all custom vars are defined as needed right ?
    Offcourse you might need to add a condition that this action only gets triggered the first time you visit the page, else all vars get reset everytime you come there.

    Math

    #348515 Score: 2
    Profile photo of Tim K
    Tim K
    Member
    contributor
    beginner
    intermediate
    advanced
    friend finder
    junior moderator
    advocate
    wise owl
    curious george
    Group Member
    181 pts
    @timk

    The trick is to put the reset action into a group on title level. It will be available on all pages and as long as you don’t Run the group nothing should be reset.

    This post has received 2 votes up.
    #348522 Score: 0
    Profile photo of Andrew Corbett
    Andrew Corbett
    Member
    intermediate
    wise owl
    advanced
    curious george
    picture perfect
    contributor
    1 pt
    @acorbett3701

    Thanks Tim. I understand now: putting Reset All Variables in a group at title level causes all the variables to be declared, but prevents the reset from actually being run.

    #348901 Score: 0
    Profile photo of Sergey Snegirev
    Sergey Snegirev
    Member
    contributor
    intermediate
    advanced
    friend finder
    lab member
    junior moderator
    advocate
    LUC16
    LUC16 Attendee
    wise owl
    337 pts
    @ssneg

    I initialize Lectora variables by mentioning them in a CONDITION of a single action (that does nothing) on the Title level. So my title is more compact.

    Also, if you use Lectora Online, you don’t have to do it. In LO, all variables are always initialized.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.