--- title: URLSearchParams.toString() slug: Web/API/URLSearchParams/toString translation_of: Web/API/URLSearchParams/toString ---

{{ApiRef("URL API")}}{{SeeCompatTable}} 

{{domxref("URLSearchParams")}} 接口的toString()方法 返回适用在URL中的查询字符串。

语法

URLSearchParams.toString()

返回值

字符串

实例

let url = new URL('https://example.com?foo=1&bar=2');
let params = new URLSearchParams(url.search.slice(1));

//Add a second foo parameter.
params.append('foo', 4);
console.log(params.toString());
//Prints 'foo=1&bar=2&foo=4'.

规范

Specification Status Comment
{{SpecName('URL', '#urlsearchparams-stringification-behavior', "toString()")}} {{Spec2('URL')}} Initial definition. Appears in the IDL as stringifier.

浏览器兼容

{{Compat("api.URLSearchParams.toString")}}

参考