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
url = "?a=2&a=3&b=2&a=4";
Url = {
get get(){
var vars= {};
if(url.length!==0)
url.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;
}
};
document.querySelector('log').innerHTML = JSON.stringify(Url.get);
<log></log>