Last active
October 12, 2017 07:10
-
-
Save nowri/1916187 to your computer and use it in GitHub Desktop.
URLからクエリ値取得
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx | |
*/ | |
<script type="text/jscript" language="javascript"> | |
function getQuerystring(key, default_) | |
{ | |
if (default_==null) default_=""; | |
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); | |
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)"); | |
var qs = regex.exec(window.location.href); | |
if(qs == null) | |
return default_; | |
else | |
return qs[1]; | |
} | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see http://www.kumagaiyusuke.info/blog/javascript%E3%81%A7%E3%82%AF%E3%82%A8%E3%83%AA%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/ | |
*/ | |
// クエリから指定したキーの値を取得する | |
function getParameterByName(name) { | |
// 変数を定義 | |
var regexStr, regex, results; | |
// キーに含まれる"["と"]"を"\["と"\]"に置換 | |
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
// キーと値を含む正規表現文字列を生成 | |
regexStr = "[\\?&]" + name + "=([^&#]*)"; | |
// 正規表現文字列から正規表現を生成 | |
regex = new RegExp(regexStr); | |
// クエリから正規表現を使って検索する | |
results = regex.exec(window.location.search); | |
// 結果がなかったとき | |
if (results === null) { | |
// 空文字列を返す | |
return ""; | |
} | |
// 結果があるとき | |
else { | |
// +を半角スペースに変更・URL文字列を通常の文字列に戻して返す | |
return decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @see http://www.kumagaiyusuke.info/blog/javascript%E3%81%A7%E3%82%AF%E3%82%A8%E3%83%AA%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/ | |
*/ | |
// クエリをobjectに変換して取得する | |
function getQueryParameters() { | |
// パラメータを定義するオブジェクト | |
var parameters = {}; | |
// 変数を定義 | |
var match, plus, search, query; | |
// +の正規表現を設定 | |
plus = /\+/g; | |
// (key)=(value)の正規表現を設定 | |
search = /([^&=]+)=?([^&]*)/g; | |
// クエリから?を除いて取得 | |
query = window.location.search.substring(1); | |
// 現在のインデックスから検索して、マッチしたら繰り返し | |
while (match = search.exec(query)) { | |
// 内容をparametersに設定 | |
parameters[decode(match[1])] = decode(match[2]); | |
} | |
// +を半角スペースに変更・URL文字列を通常の文字列に戻す | |
function decode(str) { | |
// 文字列を返す | |
return decodeURIComponent(str.replace(plus, " ")); | |
}; | |
// パラメータを定義したオブジェクトを返す | |
return parameters; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getParameterByKey(url, key) { | |
var regexStr, regex, results; | |
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); | |
regexStr = "[\\?&]" + key + "=([^&#]*)"; | |
regex = new RegExp(regexStr); | |
results = regex.exec(url); | |
if (results === null) { | |
return ""; | |
} | |
else { | |
return decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment