Skip to content

Instantly share code, notes, and snippets.

@mbaez
Created February 26, 2014 14:37
Show Gist options
  • Save mbaez/9230517 to your computer and use it in GitHub Desktop.
Save mbaez/9230517 to your computer and use it in GitHub Desktop.
This method gets the url parameters and constructs an object with them
/**
* This method gets the url parameters and constructs an object with them.
* <code>
* localhost/app?param=1&param2=2&params3=text
*
* getUrlParams() returns {param: 1, param2:2, params3='text'}
* </code>
* @author Maximiliano Báez
*/
function getUrlParams() {
var urlString = document.location.search.replace("?", "");
// remove the final #
if (urlString[urlString.length - 1] == "#") {
urlString = urlString.substr(0, urlString.length - 1);
}
var params = urlString.split("&");
var urlParams = {};
//ie trim support
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
// parse all url parmeters
for (var i = 0; i < params.length; i++) {
var tokens = params[i].split("=");
urlParams[tokens[0].trim()] = tokens[1];
}
return urlParams;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment