Created
June 22, 2014 11:15
-
-
Save JosePedroDias/e86bfec006c4e2a64245 to your computer and use it in GitHub Desktop.
to and from queryString
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 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