javascriptでGETパラメタを取り扱う
正しくはGETパラメタとは呼ばない・・・クエリ文字列と呼ぶ?
code:js
Util = {}
Util.getQueryHash = function () {
var result = {};
var query = document.location.search;
if (query.length < 1) return result;
query = query.substring(1);
var kvs = query.split('&');
for (var i = 0; i < kvs.length; i++) {
var element = kvsi.split('='); var key = decodeURIComponent(element0); var value = decodeURIComponent(element1); resultkey = decodeURIComponent(value); }
return result;
}
Util.generateQueryString = function(hash){
var result = '';
for (key in hash) {
key = encodeURIComponent(key);
var value = encodeURIComponent(hashkey); var element = key + '=' + value;
if (result.length > 1) { element = '&' + element }
result = result + element;
}
return result;
}
Util.setQueryHash = function (hash) {
document.location.search = Util.generateQueryString(hash);
}