Created
May 16, 2012 04:51
-
-
Save phalkunz/2707527 to your computer and use it in GitHub Desktop.
Function for extracting url query string variables from the `window.location`
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
/** | |
* Returns a literal object with properties mapped to the query string variables | |
* For example: | |
* input (page url): http://www.test.com/index?one=1&two=2 | |
* value: { 'one': 1, 'two': 2 } | |
*/ | |
function urlVars() { | |
var search = window.location.search; | |
if(search === '' || !search || search === '?') { | |
return null; | |
} | |
else { | |
var parts = search.substr(1, search.length - 1).split('&'), | |
props = {}; | |
for(var i = 0; i < parts.length; i++) { | |
var nameVal = parts[i].split('='); | |
props[nameVal[0]] = nameVal[1]; | |
} | |
return props; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment