Created
July 6, 2019 03:22
-
-
Save dallashuggins/da4965b1b8800f0a48019b688ecac150 to your computer and use it in GitHub Desktop.
Example of a function that gets the current URL and parses the query parameters and stores in sessionStorage.
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
// Parse query params: | |
export default () => { | |
let finalParams | |
try { | |
const paramArray = window.location.search.replace('?', '').split('&') | |
const params = {} | |
paramArray.map((param, i) => { | |
if (param) { | |
const pair = param.split('=') | |
const key = pair[0] | |
const val = pair[1] | |
params[key] = val | |
} | |
return param | |
}) | |
if (Object.entries(params).length > 0) { | |
sessionStorage.removeItem('params') | |
sessionStorage.setItem('params', JSON.stringify(params)) | |
finalParams = params | |
} else { | |
const storageParams = sessionStorage.getItem('params') | |
const parsedStorage = storageParams ? JSON.parse(storageParams) : null | |
finalParams = parsedStorage | |
} | |
} catch (e) { | |
console.log(e) | |
} | |
return finalParams | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment