Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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