URLSearchParams: size プロパティ
メモ: この機能はウェブワーカー内で利用可能です。
URLSearchParams.size
は URLSearchParams
インターフェイスの読み取り専用のプロパティで、検索引数項目の総数を示します。
値
数値で、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 GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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.
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.