LoginSignup
0
1

More than 5 years have passed since last update.

JavascriptでURLのパラメータをオブジェクトで取得する

Posted at
javascript.js
const param = window.location.search.replace(/^\?/, "").split("&").reduce((p,i)=>{const t = i.split("=");p[t[0]]=t[1];return p;},{});

Googleでhoge hogeを検索した場合以下のように取れます。

javascript.js
window.location.search.replace(/^\?/, "").split("&").reduce((p,i)=>{const t = i.split("=");p[t[0]]=t[1];return p;},{})
{q: "hoge+hoge", rlz: "1C1CHZL_jaJP770JP770", oq: "hoge+hoge", aqs: "chrome..69i57j69i61l2j0l2j69i61.1744j0j8", sourceid: "chrome", }
aqs: "chrome..69i57j69i61l2j0l2j69i61.1744j0j8"
ie: "UTF-8"
oq: "hoge+hoge"
q: "hoge+hoge"
rlz: "1C1CHZL_jaJP770JP770"
sourceid: "chrome"
__proto__: Object

テキストだと個別のパラメータにアクセスしにくいので便利。
自分用メモレベル。

0
1
1

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