Forum Discussion

DwayneDush's avatar
DwayneDush
Community Member
11 days ago
Solved

JavaScript for getting today's month

I would like to create a page in Storyline where the system identifies the current month.  Then based on that month, the page will display a unique image (i.e. the layer specific to that month).  The learner will later have the ability to select each of the other months and I know how to do that via the triggers.  But I cannot figure out the JavaScript for accessing the current system date, and then utilize that value to begin the page's display using the associated layer for the specific month.

I have tried to copy/paste various JavaScript code but none seem to be working.  And the reason I don't think it was working is because I tried testing it by displaying the value on a screen and then using the PREVIEW PAGE function to see what value had returned.  Nothing (blank in the Month field) is being displayed.  And maybe that is a different question... can I use the PREVIEW function in Storyline to execute the JS and then see the result via displaying it on a page/slide?

I am hoping that for JS programmers, this is pretty simple.

  • Hi DwayneDush if you define a variable in Storyline called currentMonth. Make it a number variable (it will initialise as zero).

    You would then place a variable change trigger on that variable, which will then determine what happens based on the value of that variable, for example, if the variable changes and it's value is "1" i.e January, you could then show layer "January", if the value is "2" then show "February" etc.

    You then just need to execute some JavaScript on the timeline start trigger that will set the Storyline variable:

    // Get the Storyline player API
    const player = GetPlayer();
    // Get the current month
    const currentMonth = new Date().getMonth() + 1; // Adding 1 because getMonth() returns 0-based index
    // Set the Storyline variable
    player.SetVar("currentMonth",currentMonth);
    

     

  • SBP_Inc's avatar
    SBP_Inc
    Community Member

    Javascript does not run in preview mode. One needs to publish the course (Web version is fine), and try check out the javascript. Yes, a bit painful

    • DwayneDush's avatar
      DwayneDush
      Community Member

      Thank you for pointing that out!  What I had might have worked but I went with the code JS provided.  And thanks to you, I went straight to the publish to test it out.  Good call!

    • PhilMayor's avatar
      PhilMayor
      Super Hero

      Javascript will now run in preview, has done for months. If it doesn't run check the console (available in preview)

  • Hi DwayneDush if you define a variable in Storyline called currentMonth. Make it a number variable (it will initialise as zero).

    You would then place a variable change trigger on that variable, which will then determine what happens based on the value of that variable, for example, if the variable changes and it's value is "1" i.e January, you could then show layer "January", if the value is "2" then show "February" etc.

    You then just need to execute some JavaScript on the timeline start trigger that will set the Storyline variable:

    // Get the Storyline player API
    const player = GetPlayer();
    // Get the current month
    const currentMonth = new Date().getMonth() + 1; // Adding 1 because getMonth() returns 0-based index
    // Set the Storyline variable
    player.SetVar("currentMonth",currentMonth);