I'm curious, what is the best practice to grab any url params from the URL ?
Example
http://localhost:8080/BIM/teacher/reports/chapter-quiz/assignment?assessmentId=206a9246&classroomId=722bfadb
The goal is to grab the value of :
- assessmentId
- classroomId
1. Make a parseUrl function
function parseUrl(url) {
var urlParamSplit = url.split("?");
if (urlParamSplit.length !== 2) {
return "InvalidUrlNoParamsSet";
}
var paramsList = urlParamSplit[1].split("&");
if (paramsList.length < 1) {
return "InvalidUrlNoParamsFound";
}
var paramsObj = {};
paramsList.forEach(function(item) {
var keyValueArray = item.split("=");
paramsObj[keyValueArray[0]] = keyValueArray[1];
});
return paramsObj;
}
var params = parseUrl(href);
console.log(params.assessmentId) // 206a9246
console.log(params.classroomId) // 722bfadb
2. Grab it from hidden input
Throw this in the HTML page
<input id="assessmentId" type="hidden" value="<c:out escapeXml="true" value="${param.assessmentId}" />" />
<input id="classroomId" type="hidden" value="<c:out escapeXml="true" value="${param.classroomId}" />" />
console.log($("#assessmentId").val()) // 206a9246
console.log($("#classroomId").val()) // 722bfadb
So far, I've tried 2 approaches,they both give me the data I wanted, but I'm not sure which one, I should use.
If there's other approach that I should consider that please kindly suggest.