Created
January 12, 2014 13:48
-
-
Save kawanet/8384773 to your computer and use it in GitHub Desktop.
QUERY_STRING をパースする JavaScript 関数。今まで何度も書いたと思うけど!
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
/** | |
* @example | |
* | |
* var param1 = parse_query_param(location.search.substr(1)); | |
* | |
* var param2 = parse_query_param(location.hash.search(/^#!.*\?/)?"":location.hash.replace(/^#!.*\?/, "")) | |
*/ | |
function parse_query_param(query) { | |
var vars = query.split(/[&;]/); | |
var param = {}; | |
for (var i = 0; i < vars.length; i++) { | |
var pair = vars[i]; | |
if (!pair.length) continue; | |
var pos = pair.indexOf("="); | |
var key, val; | |
if (pos > -1) { | |
key = pair.substring(0, pos); | |
val = pair.substring(pos + 1); | |
} else { | |
key = val = pair; | |
} | |
key = key.replace(/\+/g, " "); | |
val = val.replace(/\+/g, " "); | |
key = decodeURIComponent(key); | |
val = decodeURIComponent(val); | |
param[key] = val; | |
} | |
return param; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment