0
1

More than 3 years have passed since last update.

JavaScript でURLのクエリ文字列を取得する

Posted at

JavaScript でURLのクエリ文字列を取得する。

現在開いているページのクエリ文字列を取得する

locationオブジェクトのsearchプロパティを利用する。

const query = location.search;

正規表現を使って、URLを表す文字列からクエリ文字列を抽出する

クエリ文字列がない場合、定数queryの値は空文字となる。

const url = 'https://example.com?a=123';
const query = url.replace(/^.*?(\?|$)/, '$1');
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1