Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (50 sloc) 1.4 KB
<html>
<body>
<script>
jekalURL = {
parse : function(url){
var urlParameter = {};
var httpCheck = /(http|https):\/\/.*/;
if(!httpCheck.test(url)){
url = 'http://' + url;
}
var a = document.createElement('a');
a.href = url;
var urlParamString = a.search;
if(urlParamString.length > 1 && urlParamString.charAt(0) == '?'){
var urlParamArray = urlParamString.substring(1).split('&');
var i, len, eIdx, urlParam;
for(i=0, len=urlParamArray.length; i < len; i++){
urlParam = urlParamArray[i];
eIdx = urlParam.indexOf('=');
urlParameter[urlParam.substring(0, eIdx)] = decodeURIComponent(urlParam.substring(eIdx+1));
}
}
return urlParameter;
},
stringify : function(obj){
if(typeof obj != 'object'){
return '';
}
var urlPairs = [];
for(var key in obj){
urlPairs.push(key + '=' + encodeURIComponent(obj[key]));
}
if(urlPairs.length < 1){
return '';
}
return '?' + urlPairs.join('&');
}
};
// ex)
// purpose : "http://jekalmin.tistory.com?name=Min&age=25&sex=male" ==> "http://jekalmin.tistory.com?name=Min&address=seoul"
var params = jekalURL.parse("http://jekalmin.tistory.com?name=Min&age=25&sex=male");
console.log(params); // { name : "Min", age : 25, sex : "male" }
delete params.age;
delete params.sex;
params.address = "seoul";
console.log(params);
console.log("http://jekalmin.tistory.com" + jekalURL.stringify(params));
</script>
</body>
</html>