Skip to content

Instantly share code, notes, and snippets.

@SrChach
Created April 11, 2019 17:36
Show Gist options
  • Select an option

  • Save SrChach/de0d1ccb9d81f2635ccada65a941f946 to your computer and use it in GitHub Desktop.

Select an option

Save SrChach/de0d1ccb9d81f2635ccada65a941f946 to your computer and use it in GitHub Desktop.
Functions for get values from URL's get parameter && edit URL strings
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}
function changeUrlParameter(url, parametro, value){
let separator = '&'
if(url.indexOf("?") === -1)
separator = '?'
url = window.location.href + '&'
let regx = new RegExp('(' + parametro + '=).*?(&)');
if(!regx.test(url)){
url = url.slice(0, -1)
return `${url}${separator}${parametro}=${value}`
} else {
let newText = url.replace(regx,'$1' + value + '$2');
return newText.slice(0, -1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment