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.

We’d love to hear your thoughts on the next set of proposals for the JavaScript language. You can find a description of the proposals here.
Please take two minutes to fill out our short survey.

toString()URLSearchParams インターフェイスのメソッドで、URL での使用に適したクエリー文字列を返します。

メモ: このメソッドは、疑問符のないクエリー文字列を返します。これは、それを含む window.location.search とは異なります。

メモ: この機能はウェブワーカー内で利用可能です。

構文

js
toString()

引数

なし。

返値

疑問符を除いた文字列です。(検索パラメーターが設定されていない場合は空文字列を返します。)

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

// 2番目の foo パラメーターの追加
params.append("foo", 4);
console.log(params.toString()); // 'foo=1&bar=2&foo=4' と表示

仕様書

Specification
URL
# urlsearchparams-stringification-behavior

ブラウザーの互換性

関連項目