URL parameter parsing

susanmacnab
susanmacnab Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 35 ☆ Superstar ☆
My question has been asked a couple of times on this forum but I don't see any resolutions.

I have a header script at the top of my assignable unit to read the URL parameters upon launch of my course. The parameters are set in the LMS.

When I launch the course from my LMS, I see that the parameters are appended to a001index.html but my script runs on page2.html (which is really "Page 1" in the screen capture below - LOL!).

Is there a setting that I am missing to run the script on a001index? Alternatively - is there a better way to grab a parameter?

Thanks in advance!
Susan


Best Answer

  • susanmacnab
    susanmacnab Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 35 ☆ Superstar ☆
    Answer ✓
    BINGO - a huge gold star for you @wheels.
    Thank you so much!

    Here is my final code for anyone else who wants to do the same:
    function getUrlParam(pname) {
    	const urlParams = new URLSearchParams(parent.window.location.search);
    	return urlParams.get(pname);
    }

Answers

  • CarlJFink
    CarlJFink Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 97 ☆ Superstar ☆
    Add the script to Page 2, not the assignable unit?
  • susanmacnab
    susanmacnab Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 35 ☆ Superstar ☆
    Thank you @CarlJFink for the fast response.
    Unfortunately the parameter doesn't get appended to page2.


  • malqaseer3753
    malqaseer3753 Community Member Posts: 8 ♪ Opening Act ♪
    Try adding your script in a Meta tags extension.
  • susanmacnab
    susanmacnab Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 35 ☆ Superstar ☆
    Thank you @malqaseer3753 but that also doesn't run in a001index.
  • wheels
    wheels Florida USACommunity Member, Administrator, Moderator, Rockstar Manager Posts: 654 eLearning ROCKSTAR Admin Team
    edited May 2, 2022
    @susanmacnab instead of using the url from window.location.href try using myTop.location.href instead as input into your parsing script.

    It might even be possible to just call our getParam() function, I didn't test it but it should be in scope.

    If not, here is the function you could add it to your page:
    function getParam(param) {
      var titleMgr = getTitleMgrHandle();
      if( titleMgr )
    	return titleMgr.GetParam(param)
      else
        return null
    }
  • susanmacnab
    susanmacnab Community Member, Lectora® Accessibility Group (LAUG) Member, eLearning Brothers® Partner Posts: 35 ☆ Superstar ☆
    edited July 29, 2022
    I've finally come back to work on this and I have it working.

    Mostly.

    The trick is to use: parent.window.location.href
    function getUrlParam(pname) {
      const queryString = parent.window.location.href;
      const urlParams = new URLSearchParams(queryString);
      return urlParams.get(pname);
    }

    The problem is that it never successfully gets the first parameter in the list and I have no idea why. In the sample URL below I can get values for colour, AICC_SID and AICC_URL but myparam gives me null. If I remove my custom parameters from the LMS settings, I can get AICC_URL but AICC_SID comes back null.


    It took me quite a bit of time to realize what was going on. My workaround for now will be to have a dummy parameter at the beginning but it would be interesting to find out why this isn't working as expected.
  • wheels
    wheels Florida USACommunity Member, Administrator, Moderator, Rockstar Manager Posts: 654 eLearning ROCKSTAR Admin Team
    URLSearchParams expects the query string.

    Try this: const urlParams = new URLSearchParams(myTop.location.search);