Created
June 18, 2018 09:58
-
-
Save azettl/ca4f27b501526b3285852d4ca6227cad to your computer and use it in GitHub Desktop.
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
String.prototype.getParams = function (search) { | |
var params = null; | |
var paramsObject = new Object(); | |
if(search && search != ""){ | |
params = this.substr(this.indexOf(search) + search.length + 1, this.length); | |
return params.substr(0, ((params.indexOf("&") == "-1") ? params.length : params.indexOf("&"))); | |
} | |
params = this.substr(this.indexOf("?") + 1, this.length).split("&"); | |
for(i=0; i < params.length; i++){ | |
param = params[i].split("="); | |
paramsObject[param[0]] = param[1]; | |
} | |
return paramsObject; | |
}; | |
var url = "http://www.google.de?hl=en&test=1&why=because"; | |
var params = url.getParams(); | |
console.log(params); // {hl: "en", test: "1", why: "because"} | |
console.log(url.getParams("hl")); //en |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment