Pass URL

tomtom
tomtom Community Member Posts: 27 ♪ Opening Act ♪
I have a url test.com?userID=1111&key=111, which launch the lectora lesson

 

I neeed to be ABLE TO pass userID and the key to lectora lesson

Comments

  • wheels
    wheels Florida USACommunity Member, Administrator, Moderator, Rockstar Manager Posts: 574 eLearning ROCKSTAR Admin Team
    You can add a Run JavaScript action and get the query string parameters using JavaScript.

    See here for required JavaScript:
    https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript

    Then you can create user variables in Lectora, MyUserId and MyKey.

    In your JavaScript you can set those variables using the set method:
    (variable JavaScript names can be found in the variable manager)
    VarMyUserId.set( idParam );
    VarMyKey.set( keyParam );

    Then you will have those values available in Lectora variables to use in your course.
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    Thank you. I have followed the instruction and I got this far.

    function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
    results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }

    var userkey1 = getParameterByName('userKey');
    var sessionUnitKey1 = getParameterByName('sessionUnitKey');
    alert(userkey1 );

    I have created 2 variables in Lectora userkey and sessionunitkey

    The problem is to set the variable for Lectora .

     

    when I do this I get an error message

    Varuserkey.set( userkey1 );

    Varsessionunitkey.set( sessionunitkey1);
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    The error message that I am getting is : ReferenceError Varuserkey is not defined
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    You have to use the variables in a Lectora action. Otherwise Lectora believes they aren't needed and doesn't initialize them. If you want to use the variable later on the same page just create this action first. You can also add a group on title level with 1 action "Reset all variables". This action uses all variables at once, just don't Run this action group if you don't want to reset the variables.
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    I want to store the variable in lectora  so I can pass it later when I submit the test to a processing page. So I got the javascript to work. Now in Lectora what do I need to do
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    Add a group on title level. Add one action to the group "Reset all variables".

    This should solve the "Varuserkey is not defined" issue.
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    On the title level I am also setting running my JS file . What should happens first. And can I create a group and reset variables first and then run the JS or reverse the order in the same group?

     
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    The action in the group is not meant to be executed, it only exists to make Lectora initialize all variables, i.e. sequence is not relevant. Just put it there, your JS won't be influenced.
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    My variables does not get set.  have attached my awt file I do not know what I am doing wrong. Please advise
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    The awt is not in the zip.

    It seems you've put the "function getParameterByName(name, url) " into a "Run javascript" action. That's not advisable because the action is turned into a function again. It's better to put it into an HTML Extension (Insert > Add Web Object > HTML Extension), change the type to "Header Scripting". Click Edit, enter the function code and remove it from the action, i.e. only the function, leave the rest.

     
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
     

    I

    will try this
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    Still does not work . Attached should have awt file
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    There are two decisive typos in your code:

    "Varuserkey is not defined", because in Lectora there is no variable called "userkey". The name of your variable is "userKey".

    When using the function you need to pass the correct values:

    You wrote your url looks like this:

    test.com?userID=1111&key=111

    To "getParameterByName(name,url)" you need to pass the name of the parameter, which is "key" not "userKey". The other parameters name is "userID", exactly like it is spelled in the URL.
  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    I am trying to execute that code now. I does not always work. The JS does not seems to trigger on the page show. I am puzzled . Please help
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    Are there any javascript errors in the console? I'm not sure why it would work sometimes but not always ...

    I tested the code above and it seemed to work.

  • tomtom
    tomtom Community Member Posts: 27 ♪ Opening Act ♪
    edited January 29, 2021
    What I found If I add the content page before. So it will be the first page then nothing get stored. Is there a way to only stored upon arrival basically on the first page and then carry through the entire lessons. My lesson will be 100+ pages plus a quiz
  • timk
    timk Community Member Posts: 1,197 ♦ Idol ♦
    You can add a condition to check whether the variable already contains a userKey, i.e. check whether it contains a value that is not the initial value of the variable, e.g.
    Run Javascript Only if "userKey" Not equal to "0".
  • andrew-robertson
    andrew-robertson Community Member Posts: 184 ☆ Superstar ☆
    @wheels and @timk Thanks to you both for this discussion. It's helped me in similar situation with dealing with local servers.
    Andrew R.
    Developing content since 2010 using a mixed bag of tools.