Explore the community Forums Lectora Lectora Questions & Answers Use Javascript to pass a variable from one title to another

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #322268 Score: 0
    Profile photo of Michael Smith
    Michael Smith
    Member
    beginner
    intermediate
    wise owl
    friend finder
    curious george
    @msmith2572

    I am very new to Lectora and Javascript, but I am familiar with other coding languages.

    For the training I am conduction I am not placing the lessons into an LMS. I created a Menu title that will allow the user to select option and then launch a series of lessons (independent titles). Each lesson presents slightly different information based on the selections made in the main menu. We are trying to keep the lessons separated out in multiple titles for sustainment purposes.

    I thought I may create and set a cookie to allow me to pass the variable. This idea seemed simple, but I can’t seem to get it to work.

    another idea would be to add / get a value from the session storage.  This too seemed simple, but I can’t get it to work.

    I have a feeling I am missing a basic step.

    I added this as a HTML extension at the top level

    <script>
    function setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? “” : “; expires=” + exdate.toUTCString());
    document.cookie = c_name + “=” + c_value;
    }
    function getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(“;”);
    for (i = 0; i < ARRcookies.length; i++) {
    x = ARRcookies[i].substr(0, ARRcookies[i].indexOf(“=”));
    y = ARRcookies[i].substr(ARRcookies[i].indexOf(“=”) + 1);
    x = x.replace(/^\s+|\s+$/g, “”);
    if (x == c_name) {
    return unescape(y);
    }
    }
    }
    function cssSelected() {
    var cssSelected = $(‘#myList’)[0].value;
    if (cssSelected !== “select”) {
    setCookie(“selectedCSS”, cssSelected, 3);
    }
    }
    $(document).ready(function() {
    $(‘#myList’)[0].value = getCookie(“selectedCSS”);
    })
    </script>

     

    Then call one of the functions on a button press using a javascript

    setCookie(“Role”, 3, 1);

     

    I have tried several variations of this, but no luck.

    #322281 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
    333 pts
    @ssneg

    Your cookie setting/getting functionality seems to work fine: https://jsfiddle.net/t88Lnmov/
    Maybe there is a problem with the cssSelected function. I cannot say because it queries your HTML, which you aren’t sharing. In particular, it is important to understand what element you are trying to get by the #myList id?

    #322474 Score: 0
    Profile photo of Michael Smith
    Michael Smith
    Member
    beginner
    intermediate
    wise owl
    friend finder
    curious george
    @msmith2572

    A should not have added that function as I am not calling it.  I have been able to get the menu title to write the cookie. by loading the “setCookie” function and the “getCookie” function into a my.js file that I use a <script> my.js</script> in the header. then placing a javascript action on a menu selection:

    setCookie(“Role”, “3”, 1);

    I am just having a problem getting the target title to read the cookie..

    I placed the same <script>my.js</script> in the header of the target lesson then use a Run a Javascript action as the first action under the book/ title.  The javascript action reads as:

    var _Role = getCookie(“Role”);

    my user variable name in the lectora title that I am trying to set is “_Role” the variable in the cookie is called “Role”

    The target titles are place in a subfolder of the menu so they should be able to access the cookie (or at least that is my understanding).

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

You must be logged in to reply to this topic.