最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript URL searchParams not returning anything - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 5

To 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);

发布评论

评论列表(0)

  1. 暂无评论