URLSearchParams

URLSearchParams interface defines utility methods to work with the query string of a URL.

An object implementing URLSearchParams can directly be used in a for...of structure, for example the following two lines are equivalent:

for (const [key, value] of mySearchParams) {}
for (const [key, value] of mySearchParams.entries()) {}
					
注意: 此特征可用于 Web 工作者 .

构造函数

URLSearchParams()
返回 URLSearchParams object instance.

方法

URLSearchParams.append()
Appends a specified key/value pair as a new search parameter.
URLSearchParams.delete()
Deletes the given search parameter, and its associated value, from the list of all search parameters.
URLSearchParams.entries()
Returns an iterator allowing iteration through all key/value pairs contained in this object.
URLSearchParams.forEach()
Allows iteration through all values contained in this object via a callback function.
URLSearchParams.get()
Returns the first value associated with the given search parameter.
URLSearchParams.getAll()
Returns all the values associated with a given search parameter.
URLSearchParams.has()
返回 Boolean indicating if such a given parameter exists.
URLSearchParams.keys()
Returns an iterator allowing iteration through all keys of the key/value pairs contained in this object.
URLSearchParams.set()
Sets the value associated with a given search parameter to the given value. If there are several values, the others are deleted.
URLSearchParams.sort()
Sorts all key/value pairs, if any, by their keys.
URLSearchParams.toString()
Returns a string containing a query string suitable for use in a URL.
URLSearchParams.values()
Returns an iterator allowing iteration through all values of the key/value pairs contained in this object.

范例

var paramsString = "q=URLUtils.searchParams&topic=api";
var searchParams = new URLSearchParams(paramsString);
//Iterate the search parameters.
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") === null; // 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"
					

Gotchas

URLSearchParams constructor does not parse full URLs. However, it will strip an initial leading ? off of a string, if present.

var paramsString1 = "http://example.com/search?query=%40";
var searchParams1 = new URLSearchParams(paramsString1);
searchParams1.has("query"); // false
searchParams1.has("http://example.com/search?query"); // true
searchParams1.get("query"); // null
searchParams1.get("http://example.com/search?query"); // "@" (equivalent to decodeURIComponent('%40'))
var paramsString2 = "?query=value";
var searchParams2 = new URLSearchParams(paramsString2);
searchParams2.has("query"); // true
var url = new URL("http://example.com/search?query=%40");
var searchParams3 = new URLSearchParams(url.search);
searchParams3.has("query") // true
					

规范

规范 状态 Comment
URL
The definition of 'URLSearchParams' in that specification.
实时标准 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
URLSearchParams Chrome 49 Edge 17 Firefox 29
29
Prior to version 57 single quotes in URLs were escaped (see bug 1386683 ).
IE No Opera 36 Safari 10.1 WebView Android 49 Chrome Android 49 Firefox Android 29
29
Prior to version 57 single quotes in URLs were escaped (see bug 1386683 ).
Opera Android 36 Safari iOS 10.3 Samsung Internet Android 5.0
URLSearchParams() 构造函数 Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari 10.1 WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS 10.3 Samsung Internet Android 5.0
append Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari 10.1 WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS 10.3 Samsung Internet Android 5.0
delete Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari 部分支持 Partial
部分支持 Partial
Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022 .
WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS 部分支持 Partial
部分支持 Partial
Removing a non-existent query parameter doesn't remove ? from the URL. See bug 193022 .
Samsung Internet Android 5.0
entries Chrome 49 Edge 17 Firefox 44 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 44 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
forEach Chrome 49 Edge 17 Firefox 44 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 44 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
get Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
getAll Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
has Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
keys Chrome 49 Edge 17 Firefox 44 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 44 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
set Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
sort Chrome 61 Edge 17 Firefox 54 IE No Opera 48 Safari Yes WebView Android 61 Chrome Android 61 Firefox Android 54 Opera Android 45 Safari iOS Yes Samsung Internet Android 8.0
toString Chrome 49 Edge 17 Firefox 29 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0
values Chrome 49 Edge 17 Firefox 44 IE No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 44 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0

图例

完整支持
完整支持
部分支持
部分支持
不支持
不支持
见实现注意事项。

另请参阅