Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Created June 22, 2014 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/e86bfec006c4e2a64245 to your computer and use it in GitHub Desktop.
Save JosePedroDias/e86bfec006c4e2a64245 to your computer and use it in GitHub Desktop.
to and from queryString
var parseQueryString = function(url) {
var aParams = {};
if (url.match(/\?(.+)/i)) {
var queryStr = url.replace(/^(.*)\?([^\#]+)(\#(.*))?/g, '$2');
if (queryStr.length > 0) {
var i, I, pairVar, aQueryStr = queryStr.split(/[;&]/);
I = aQueryStr.length;
for (i = 0; i < I; ++i) {
pairVar = aQueryStr[i].split('=');
aParams[decodeURIComponent(pairVar[0])] = (typeof(pairVar[1]) !== 'undefined' && pairVar[1]) ? decodeURIComponent(pairVar[1]) : '';
}
}
}
return aParams;
};
var toQueryString = function(o) {
var res = [];
var keys = Object.keys(o);
keys.forEach(function(key) {
var val = o[key];
res.push( [encodeURIComponent(key), encodeURIComponent(val)].join('=') );
});
return '?' + res.join('&');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment