URL parameter parsing
April 29, 2022 12:00 AM
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
Solutions (4)
Thank you @CarlJFink for the fast response.
Unfortunately the parameter doesn't get appended to page2.
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);
}
Add the script to Page 2, not the assignable unit?
Try adding your script in a Meta tags extension.
Discussion (8)
Try adding your script in a Meta tags extension.
Thank you @CarlJFink for the fast response.
Unfortunately the parameter doesn't get appended to page2.
Thank you @malqaseer3753 but that also doesn't run in a001index.
@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
}
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.
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);
}
URLSearchParams expects the query string.
Try this: const urlParams = new URLSearchParams(myTop.location.search);