Skip to content

Instantly share code, notes, and snippets.

@iArnaud
Forked from MarkVaughn/get_set_url_param.js
Last active August 29, 2015 13:58
Show Gist options
  • Save iArnaud/9931337 to your computer and use it in GitHub Desktop.
Save iArnaud/9931337 to your computer and use it in GitHub Desktop.
function getURLParameter(name) {
return decodeURIComponent(
(RegExp('[?|&]'+name + '=' + '(.+?)(&|$)').exec(location.search)||[null,null])[1]
);
}
function setURLParameter(name,value){
var search,
getParam = getURLParameter(name),
isParamInUrl = (getParam !== null && getParam !== "" && getParam !== "null" && (typeof getParam === "string" && getParam.length > 0));
if(isParamInUrl){
search =location.search.replace(new RegExp('([?|&]'+name + '=)' + '(.+?)(&|$)'),"$1"+encodeURIComponent(value)+"$3");
}else if(location.search.length){
search = location.search +'&'+name + '=' +encodeURIComponent(value);
}else{
search = '?'+name + '=' +encodeURIComponent(value);
}
History.pushState({state:History.getStateId()+1},document.title,search);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment