
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:03 AM
JavaScript Gurus,
Based on the SNCGuru article here:
https://servicenowguru.com/scripting/client-scripts-scripting/parse-url-parameters-client-script/
I see the following short script:
function getParameterValue(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(top.location);
if (results == null) {
return "";
} else {
return unescape(results[1]);
}
}
...But the script editor is complaining of some errors on the "name.replace" line...
Can someone help me fix up this script so there are no errors or warnings?
I'm not totally clear on what the Intention was. Thank you!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:36 AM - edited 10-26-2023 09:39 AM
Hi @G24,
The intention was to escape characters which should normally be escaped in a regex. However because of the way the characters are used here, there's no need to escape.
unescape is a deprecated function. You should use decodeURI instead.
You can use it like this:
function getParameterValue(name) {
name = name.replace(/[[]/, "\\[").replace(/[\]]/, "\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(top.location);
if (results == null) {
return "";
} else {
return decodeURI(results[1]);
}
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2023 09:36 AM - edited 10-26-2023 09:39 AM
Hi @G24,
The intention was to escape characters which should normally be escaped in a regex. However because of the way the characters are used here, there's no need to escape.
unescape is a deprecated function. You should use decodeURI instead.
You can use it like this:
function getParameterValue(name) {
name = name.replace(/[[]/, "\\[").replace(/[\]]/, "\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(top.location);
if (results == null) {
return "";
} else {
return decodeURI(results[1]);
}
}
Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.