Last active
December 19, 2015 01:09
-
-
Save allensarkisyan/5873977 to your computer and use it in GitHub Desktop.
Parses a query string or location.search into an Object.
This file contains hidden or 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
/** | |
* @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