Created
July 15, 2013 08:53
-
-
Save fadomire/5998474 to your computer and use it in GitHub Desktop.
get / set / delete url parameter javascript
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
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