request.getParameter returns dirty string

Viewed 52

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:

https://test.se/test.html?language=sv&SubjectArea=Ekonomi&identity=programSV,coursesSV,lifeLongLearningCoursesSV&showbutton=false

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?

2 Answers

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

Related