Problem:
I wanted to easily grab all url params and convert it to a
json object. Many of my projects requires me to use querystrings, but is always a pain trying to parse it.
Solution:
Using location.search and regex, I’m able to achieve this easily. I wrapped it in a function so you can easily drop it into your projects.
function UrlParamsToJson(){ var queryString = location.search.substring(1); var obj = {}; if(queryString) { obj = JSON.parse('{"' + decodeURI(queryString) .replace(/"/g, '\\"') .replace(/&/g, '","') .replace(/=/g, '":"') + '"}'); } return obj; }
Hope this helps somebody. Onward!
References:
Comments
Post a Comment