Javascript convert query string to JSON using jQuery and back to query string [closed]
Working, but if you used url || location.search
then you should remove if(url === '') return ''
or it should be like,
var queryStringToJSON = function (url) { url = url || location.search;// url or location.search if (url === '') return '';// return if url and location.search not found // your remainig code}
Alternatively... Instead of:
for (var idx in pairs) { var pair = pairs[idx].split('='); if (!!pair[0]) result[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || '');}
Try:
result = pairs.reduce(function(a,b) { var pair = b.split("="); a[pair[0].toLowerCase()] = decodeURIComponent(pair[1] || ''); return a;},{});
Works the same but uses reduce()
which being a native javascript function, is probably faster.