URLSearchParams:toString() 方法

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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

URLSearchParams 接口的 toString() 方法返回适用于 URL 中的查询字符串。

备注: 方法返回不带问号的查询字符串。这与 Location.searchHTMLAnchorElement.searchURL.search 都不同,它们均带有问号。

语法

js
toString()

参数

无。

返回值

一个不带问号的字符串(如果未设置查询参数,则返回空字符串)。

示例

js
const url = new URL("https://example.com?foo=1&bar=2");
const params = new URLSearchParams(url.search);

// 添加第二个参数。
params.append("foo", 4);
console.log(params.toString()); // 输出“foo=1&bar=2&foo=4”

规范

Specification
URL
# urlsearchparams-stringification-behavior

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
toString

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见