admin管理员组文章数量:1308343
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);
本文标签: JavaScript URL searchParams not returning anythingStack Overflow
版权声明:本文标题:JavaScript URL searchParams not returning anything - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741859875a2401559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论