Last active
September 23, 2015 22:08
-
-
Save nenjiru/623468 to your computer and use it in GitHub Desktop.
クエリーストリングをハッシュに変換
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
/** | |
* Query string convert to hash. | |
* | |
* @param {String} string ?を含んだ文字列 | |
* @return {Object} | |
* | |
* @example | |
* toObject("index.html?width=500&height=auto"); | |
* {width:500, height:auto} | |
*/ | |
function toobject(string) | |
{ | |
var params, | |
i = 0, | |
q, | |
kv, | |
result = {}; | |
string = string.replace(/^\?/, ''); | |
params = string.split('&'); | |
for (;q = params[i++];) { | |
kv = q.split('='); | |
//ブレース[]含んでたら配列にする | |
if (decodeuricomponent(kv[0]).match(/\[\]/)) | |
{ | |
if (!result[kv[0]]) | |
{ | |
result[kv[0]] = []; | |
} | |
result[kv[0]].push(decodeuricomponent(kv[1])); | |
} | |
else | |
{ | |
result[kv[0]] = decodeuricomponent(kv[1]); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment