Skip to content

Instantly share code, notes, and snippets.

@fadomire
Created July 15, 2013 08:53
Show Gist options
  • Save fadomire/5998474 to your computer and use it in GitHub Desktop.
Save fadomire/5998474 to your computer and use it in GitHub Desktop.
get / set / delete url parameter javascript
getUrlParameter: function (name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
},
setUrlParameter: function (name, value) {
var search;
if(this.getUrlParameter(name)){
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);
}
//router.navigate(location.pathname + search, {trigger: true});
},
deleteUrlParameter: function (name) {
var search;
if(this.getUrlParameter(name)) {
search = location.search.replace(new RegExp('([?|&]'+name + '=)' + '[^&]+'),"");
if (search.indexOf('?') === -1) {
search = '?' + search;
}
} else {
search = '';
}
//router.navigate(location.pathname + search);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment