I made this variation of gnarf's solution, so the call and the result is similar to PHP:
function S_GET(id){
var a = new RegExp(id+"=([^&#=]*)");
return decodeURIComponent(a.exec(window.location.search)[1]);
}
But as being called in a function slows the process, its better to use as global:
window['
var_name'] = decodeURIComponent( /
var_in_get=([^&#=]*)/.exec(window.location.search)[1] );
UPDATE
As I'm still learning JS, I created a better answer in a more JS behaviour:
Url = {
get get(){
var vars= {};
if(window.location.search.length!==0)
window.location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value){
key=decodeURIComponent(key);
if(typeof vars[key]==="undefined") {vars[key]= decodeURIComponent(value);}
else {vars[key]= [].concat(vars[key], decodeURIComponent(value));}
});
return vars;
}
};
This allows to be called just using Url.get
.
Example
The url ?param1=param1Value¶m2=param2Value
can be called like:
Url.get.param1 //"param1Value"
Url.get.param2 //"param2Value"
here is a snipet:
// URL GET params_x000D_
url = "?a=2&a=3&b=2&a=4";_x000D_
_x000D_
Url = {_x000D_
get get(){_x000D_
var vars= {};_x000D_
if(url.length!==0)_x000D_
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value){_x000D_
key=decodeURIComponent(key);_x000D_
if(typeof vars[key]==="undefined") {vars[key]= decodeURIComponent(value);}_x000D_
else {vars[key]= [].concat(vars[key], decodeURIComponent(value));}_x000D_
});_x000D_
return vars;_x000D_
}_x000D_
};_x000D_
_x000D_
document.querySelector('log').innerHTML = JSON.stringify(Url.get);
_x000D_
<log></log>
_x000D_