Skip to content

Instantly share code, notes, and snippets.

@phalkunz
Created May 16, 2012 04:51
Show Gist options
  • Save phalkunz/2707527 to your computer and use it in GitHub Desktop.
Save phalkunz/2707527 to your computer and use it in GitHub Desktop.
Function for extracting url query string variables from the `window.location`
/**
* 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