Created
February 26, 2014 14:37
-
-
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 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
/** | |
* This method gets the url parameters and constructs an object with them. | |
* <code> | |
* localhost/app?param=1¶m2=2¶ms3=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