Last active
June 7, 2021 12:37
-
-
Save paulmwatson/49b9f25aa75726764bcacc6febdd6247 to your computer and use it in GitHub Desktop.
Manipulating the URL via 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
let url = 'https://host.test/path/here.filetype?query=string&more=params#anchor'; | |
const querystringStart = url.indexOf('?'); | |
const anchorStart = url.indexOf('#'); | |
const querystring = url.slice(querystringStart + 1, anchorStart); | |
let querystringParams = querystring.split('&'); | |
let newQuerystringParams = []; | |
querystringParams.forEach((queryparam, i) => { | |
let param = queryparam.split('='); | |
if (param[0] !== 'more') { | |
newQuerystringParams.push(`${param[0]}=${param[1]}-changed`) | |
} | |
}); | |
newQuerystringParams.push('new=param') | |
const newQuerystring = newQuerystringParams.join('&'); | |
const newUrl = `${url.slice(0, querystringStart)}?${newQuerystring}${url.slice(anchorStart)}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment