URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串。

一个实现了 URLSearchParams 的对象可以直接用在 for...of 结构中,例如下面两行是相等的。

for (var p of mySearchParams);
for (var p of mySearchParams.entries());

Note: 此特性在 Web Worker 中可用。

构造函数

URLSearchParams()
返回一个 URLSearchParams 对象。

属性

该接口不继承任何属性。

方法

该接口不继承任何属性。

URLSearchParams.append()
 插入一个指定的键/值对作为新的搜索参数。
URLSearchParams.delete()
 从搜索参数列表里删除指定的搜索参数及其对应的值。
URLSearchParams.entries()
 返回一个iterator可以遍历所有键/值对的对象。
URLSearchParams.get()
 获取指定搜索参数的第一个值。
URLSearchParams.getAll()
 获取指定搜索参数的所有值,返回是一个数组。
URLSearchParams.has()
 返回 Boolean 判断是否存在此搜索参数。
URLSearchParams.keys()
返回iterator 此对象包含了键/值对的所有键名。
URLSearchParams.set()
 设置一个搜索参数的新值,假如原来有多个值将删除其他所有的值。
URLSearchParams.sort()
 按键名排序。
URLSearchParams.toString()
 返回搜索参数组成的字符串,可直接使用在URL上。
URLSearchParams.values()
 返回iterator 此对象包含了键/值对的所有值。

示例

var paramsString = "q=URLUtils.searchParams&topic=api"
var searchParams = new URLSearchParams(paramsString);

for (let p of searchParams) {
  console.log(p);
}

searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === ""; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"

规范

Specification Status Comment
URL
URLSearchParams
Living Standard 初次定义

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
URLSearchParamsChrome Full support 49Edge Full support 17Firefox Full support 29
Notes
Full support 29
Notes
Notes Prior to version 57 single quotes in URLs were escaped (see bug 1386683).
IE No support NoOpera Full support 36Safari Full support 10.1WebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29
Notes
Full support 29
Notes
Notes Prior to version 57 single quotes in URLs were escaped (see bug 1386683).
Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
URLSearchParams() constructorChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
appendChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
deleteChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Partial support Partial
Notes
Partial support Partial
Notes
Notes Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022.
WebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Partial support Partial
Notes
Partial support Partial
Notes
Notes Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022.
Samsung Internet Android Full support Yes
entriesChrome Full support 49Edge Full support 17Firefox Full support 44IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 44Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
forEachChrome Full support 49Edge Full support 17Firefox Full support 44IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 44Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
getChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
getAllChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
hasChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
keysChrome Full support 49Edge Full support 17Firefox Full support 44IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 44Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
setChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
sortChrome Full support 61Edge Full support 17Firefox Full support 54IE No support NoOpera Full support 48Safari Full support YesWebView Android Full support 61Chrome Android Full support 61Edge Mobile No support NoFirefox Android Full support 54Opera Android Full support 45Safari iOS Full support YesSamsung Internet Android Full support Yes
toStringChrome Full support 49Edge Full support 17Firefox Full support 29IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 29Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes
valuesChrome Full support 49Edge Full support 17Firefox Full support 44IE No support NoOpera Full support 36Safari Full support YesWebView Android Full support 49Chrome Android Full support 49Edge Mobile No support NoFirefox Android Full support 44Opera Android Full support 36Safari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
See implementation notes.
See implementation notes.

参见




























 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

文档标签和贡献者

标签: 
此页面的贡献者: mdnwebdocs-bot, zhangchen, xianshenglu, lovue, scscms, teoli, ziyunfei
最后编辑者: mdnwebdocs-bot,