Created
April 11, 2019 17:36
-
-
Save SrChach/de0d1ccb9d81f2635ccada65a941f946 to your computer and use it in GitHub Desktop.
Functions for get values from URL's get parameter && edit URL strings
This file contains hidden or 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
| 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