URLSearchParams: size プロパティ

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

URLSearchParams.sizeURLSearchParams インターフェイスの読み取り専用のプロパティで、検索引数項目の総数を示します。

数値で、URLSearchParams オブジェクト内の検索引数項目の総数を示します。

検索引数項目数の取得

検索引数項目の総数を取得するには、次のようにします。

js
const searchParams = new URLSearchParams("c=4&a=2&b=3&a=1");
searchParams.size; // 4

a 引数が 2 回与えられますが、 size は 3 ではなく、与えられたすべての項目の数 (4) を返すことに注意してください。固有のキーの数を取得するには、Set などを使用してください。

js
[...new Set(searchParams.keys())].length; // 3

検索引数が存在するかどうかをチェック

size` プロパティは、検索引数があるかどうかをチェックするのに便利です。

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

if (url.searchParams.size) {
  console.log("URL has search parameters!");
}

仕様書

Specification
URL
# dom-urlsearchparams-size

ブラウザーの互換性

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
size

Legend

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

Full support
Full support
No support
No support
Has more compatibility info.

関連情報