Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Parses a query string or location.search into an Object.
/**
* @name - parseQueryString
* @author - Allen Sarkisyan
* @license - Open Source MIT License
*
* @description - Parses a query string into an Object.
* - Optionally can also parse location.search by invoking without an argument
*/
function parseQueryString(queryString) {
var obj = {};
function sliceUp(x) { x.replace('?', '').split('&').forEach(splitUp); }
function splitUp(x) { var str = x.split('='); obj[str[0]] = decodeURIComponent(str[1]); }
try { sliceUp((!queryString ? location.search : queryString)); } catch(e) {}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.