URL.searchParams

URL 接口的 searchParams 属性返回一个 URLSearchParams 对象,这个对象包含当前 URL 中解码后的 GET 查询参数。

备注: 此特性在 Web Worker 中可用。

一个 URLSearchParams 对象。

示例

如果你的 URL 是 https://example.com/?name=Jonathan%20Smith&age=18,你可以这样解析 URL,然后得到 nameage 的值。

js
let params = new URL(document.location).searchParams;
let name = params.get("name"); // is the string "Jonathan Smith".
let age = parseInt(params.get("age")); // is the number 18

规范

Specification
URL Standard
# dom-url-searchparams

浏览器兼容性

BCD tables only load in the browser