LoginSignup
2
1

More than 5 years have passed since last update.

表示ページのGETパラメータをObjectで取得する方法

Posted at

死ぬほど長いクエリパラメータがあるページの調査で使用しました。

GETパラメータに対して処理するロジックでも使用できると思います。

コード

code
function getQueryParams() {
  var params = {};
  var url = window.location.href;

  url.replace(/([^(?|#)=&]+)(=([^&]*))?/g, function($0,$1, $2, $3) {
    if (typeof $3 === 'undefined') return;
    params[$1] = $3;
  });

  return params;
}

// 取得
var params = getQueryParams();
結果
{
   type: "",
   tag_id: "",
   tag_off: "",
   dcolor: "1",
   dstore: "1",
   p_isize: "2",
}
2
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
2
1