LoginSignup
9
1

More than 5 years have passed since last update.

javascriptでクエリストリングを取得する

Last updated at Posted at 2018-02-14

クエリストリングは以下の方法で取得できます。

const params = window.location.search

ただ、取り出しただけではkey=value&key=valueで繋がれた状態で使用しにくいので、
Node.js標準ライブラリのquerystringparseを使用して扱いやすいオブジェクトの形にパースします。

import { parse } from 'querystring'
console.log(parse("param1=hoge&param2=fuga")) // { param1: 'hoge', param2: 'fuga' }

これを使ってクエリストリングを下の方法で取得します。

const params = parse(window.location.search.slice(1))
// window.location.search のままだと先頭に?がついているためそれを削除する必要があります。

=とか&をゴリゴリっとして頑張って作っているのを見かけたので調べました。

9
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
9
1