Forum Discussion

JoshuaGarlick-4's avatar
JoshuaGarlick-4
Community Member
3 years ago

JavaScript Issue - Local vs. Global Scope in user.js

Hello,

While Articulate is not able to provide Javascript support, I am hoping someone from these forums can help me.

At the end of my activity, I have a WebObject that embeds a local .html containing a button and some JavaScript code. The code is such that when the user clicks the button, Storyline variables are passed into the browser URL.

The variables that are to be passed are first set in some custom JavaScript within Storyline. (See left side of attached image)

However, once the course is published and the user.js is generated, my custom JavaScript is scoped locally to an automatically generated function called Script3. (See right side of attached image) Because it is scoped locally and not globally, this code is rendered inaccessible to my WebObject.

There is a fix that involves moving the Script3 end bracket up so that my getVariable functions are globally scoped.

This can get tedious during testing, so I am wondering if anyone might have a better fix. Is there any way to control how code is scoped when the user.js file is generated?

Thank you for any help!

-Josh 

  • So got that last part fixed now...
    Check here for Review version...
    https://360.articulate.com/review/content/d02ea9a4-b779-4447-befa-7af5a2d02e21/review

    Im gonna make a post of it after cleaning it up with some explanation how things are done... That will be tomorrow at the earliest however...

    To test it... use the inputfields to add some property and its value... click 'set global Value'... 'get Value' on the first page i have to remove...it only shows console.logs..

    Click the next arrow to go to a second page...

    As the property name and value are Storyline variables, they stay in the input fields. You can change the value...or add new properties with their values and click 'set global value' again..

    Go back and forth and change/add as many properties as you want...
    When done click 'show global Object data' and in the grey field all data you entered willl show.

    You can edit and change it anytime...


  • Math,

    This is brilliant! Thank you! I cannot thank you enough for taking the time out of your day to put together such a detailed explanation of your solution. It is immensely helpful to me and the team I work with. I'll let you know if I have any questions, but this seems to solve the issue that we are having. 

    With much appreciation,

    -Josh 

  • I use a Webobject to create a globals in the HTML. Inthere mostly functions...but these i can call from anywhere in Storyline. If you share a sample...im quite sure we will be able to set it up so your issue will be solved.

    • JoshuaGarlick-4's avatar
      JoshuaGarlick-4
      Community Member

      Math,

      Thank you so much for offering to help fix this issue!

      I am very interested in hearing about how you use functions with WebObjects.

      I've completely paired down the attached activity to just two slides. The first slide executes the JavaScript and the second slide contains the WebObject credit button. 

      I've also attached the index.html that the WebObject uses.

      Please let me know if you need anything else.

      Thanks again for your help!

      Best,

      -Josh

    • JoshuaGarlick-4's avatar
      JoshuaGarlick-4
      Community Member

      Hi Math,

      Sorry to bother you, I'm wondering if you've had a chance to look at the attached files? Thanks very much,

      -Josh

      • MathNotermans-9's avatar
        MathNotermans-9
        Community Member

        Hi Joshua, No problem, i did and couldnot solve it ( yet ). Mainly due to other projects having priority :-(
        I will try to send a sample of how to make things global soon. 

  • Basically have a good setup working now for global setting and getting variables. And guess that will work for you too. Tonight im continuing with this so it clearly shows the possibilities. Only thing missing now is parsing the object received and show it in Storyline.... now only console.logs..