Skip to content

Instantly share code, notes, and snippets.

@dallashuggins
Created July 6, 2019 03:22
Show Gist options
  • Save dallashuggins/da4965b1b8800f0a48019b688ecac150 to your computer and use it in GitHub Desktop.
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.
// 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