URLSearchParams.delete() method in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2022 10:26 PM
Hi,
I am trying to use URLSearchParams.delete() method and I am getting a compilation error.
Error is
"Could not save record because of a compile error: JavaScript parse error at line (25) column (22) problem = missing name after . operator (<refname>; line 25)"
Code is
var url = top.location.href;
var params = new URLSearchParams(url);
params.delete('foo');
Has anyone seen this issue or know how to fix this issue, please let me know.
Thanks
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-26-2022 10:58 PM
Hi
The delete()
method of the URLSearchParams
interface deletes the given search parameter and all its associated values, from the list of all search parameters.
Syntax: delete(name)
name
The name of the parameter to be deleted.
Here is an example :
let url = new URL('https://example.com?foo=1&bar=2&foo=3');
let params = new URLSearchParams(url.search);
// Delete the foo parameter.
params.delete('foo'); //Query string is now: 'bar=2'
Mark my answer correct & Helpful, if Applicable.
Thanks,
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 02:56 PM
Did you ever figure this out? I'm running into the same issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 11:11 PM
Hey, I'm facing this issue too, any workaround ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2025 11:31 PM
Your error is happening because you're passing the full URL (top.location.href) directly to URLSearchParams. The URLSearchParams constructor expects only the query string (the part after ? in a URL), not the full URL.
var url = new URL(top.location.href); // Parse the full URL
var params = new URLSearchParams(url.search); // Extract only the query string
params.delete('foo'); // Remove the parameter
// If you need to update the URL without reloading:
url.search = params.toString();
top.history.replaceState(null, '', url.toString());
HOPE THIS HELP... Please MARK CORRECT OR HELPFUL!!!!