Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

URLSearchParams.delete() method in ServiceNow

Pompha
Mega Contributor

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

7 REPLIES 7

Community Alums
Not applicable

Hi @Pompha ,

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

 

Andrew96
Tera Contributor

Did you ever figure this out? I'm running into the same issue. 

Adeep
Tera Contributor

Hey, I'm facing this issue too, any workaround ?

Mohammed_Iqbal
Tera Guru

Hi @Pompha @Adeep @Andrew96 ,

 

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!!!!