I have a site which is made by a CMS. When the site is visited as normal and a user search on the site with the URL:
The JS code:
let test = requester.getParameter("identity")
console.log(test)
// programSV,coursesSV,lifeLongLearningCoursesSV
returns programSV,coursesSV,lifeLongLearningCoursesSV as expected.
But when I visit the site at the exact same URL but I come from an external page JS code above returns this instead: programSV%2CcoursesSV%2ClifeLongLearningCoursesSV
Any suggestions on what could be wrong and how this could be fixed?
Use a URL search params for consistency - it will decode the URL for you regardless of encoded entities
Alternatively use decodeURIComponent
const url1 = new URL(`https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV,coursesSV,lifeLongLearningCoursesSV&showbutton=false`)
const url2 = new URL(`https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV%2CcoursesSV%2ClifeLongLearningCoursesSV&showbutton=false`)
console.log(url1.searchParams.get("identity"))
console.log(url2.searchParams.get("identity"))
// alternative
console.log(decodeURIComponent(`programSV%2CcoursesSV%2ClifeLongLearningCoursesSV`))
NOTE: decodeURI does nothing for your commas:
decodeURI(requester.getParameter("identity"))
returns programSV%2CcoursesSV%2ClifeLongLearningCoursesSV
however decodeURIComponent does work:
decodeURIComponent(requester.getParameter("identity"))
returns programSV,coursesSV,lifeLongLearningCoursesSV
the commas are being encoded, you have to use decodeURIComponent. Like this: decodeURIComponent(requester.getParameter("identity")). See link for reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent