I'm trying to run this in the latest version of Chrome (75.0.3770.142), but I'm not getting any search parameters. For example:
let params = (new URL("")).searchParams;
This is just returning an empty object, so the query string hasn't been parsed. I've tried multiple examples but they all return an empty object. Am I doing something wrong here?
I'm trying to run this in the latest version of Chrome (75.0.3770.142), but I'm not getting any search parameters. For example:
let params = (new URL("http://blah.?q=something")).searchParams;
This is just returning an empty object, so the query string hasn't been parsed. I've tried multiple examples but they all return an empty object. Am I doing something wrong here?
Share Improve this question asked Aug 14, 2019 at 12:54 TonyTony 3,6389 gold badges50 silver badges80 bronze badges 2-
1
.searchParams.get("q")
– user7290573 Commented Aug 14, 2019 at 12:57 - This is a duplicate of URLSearchParams returning null for the first query string – Baljinder Singh Commented Aug 14, 2019 at 13:05
2 Answers
Reset to default 5To get all URL parameters, try using searchParams.toString()
:
var params = (new URL('http://blah.?q=something')).searchParams.toString();
console.log(params);
To get a specific value by a parameter name, use searchParams.get('q')
:
var params = (new URL('http://blah.?q=something')).searchParams.get('q');
console.log(params);
You can use:
const url = "https://www.google./search?q=test&rlz=1C1CHBF_enVN867VN867&oq=test&aqs=chrome..69i57j69i60l4j69i61j69i60j69i65.1415j0j7&sourceid=chrome&ie=UTF-8";
const foo = new URL(url);
const searchParams = Object.fromEntries(Array(...foo.searchParams.entries()));
console.log(searchParams);