Created
April 6, 2013 06:45
-
-
Save k-motoyan/5325157 to your computer and use it in GitHub Desktop.
javascriptでquery stringを連想配列で取得する関数
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
var getQueryString = function(option) { | |
var delimiter; | |
if (!option || !option.hasOwnProperty('delimiter')) { | |
delimiter = '&'; | |
} else { | |
delimiter = option.delimiter; | |
} | |
var slice_point = window.location.href.indexOf('?'); | |
if (slice_point < 0) { | |
console.log("Not found query String."); | |
return null; | |
} | |
var url_params = window.location.href.slice(slice_point + 1).split(delimiter); | |
var query_strings = {}; | |
for(var i in url_params) { | |
var query_string = url_params[i].split('='); | |
query_strings[query_string[0]] = query_string[1]; | |
} | |
return query_strings; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't handle when a query string doesn't have a value, it should then return
null
.See: https://github.com/sindresorhus/query-string