Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created July 27, 2018 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/b4659c847970de48a934f3c42448a611 to your computer and use it in GitHub Desktop.
Save hubgit/b4659c847970de48a934f3c42448a611 to your computer and use it in GitHub Desktop.
URLSearchParams
(() => {
// build
const params = new URLSearchParams()
params.append('q', 'bar')
params.append('n', 5)
// stringify
// const url = `https://example.com/resources?${params.toString()}`
const location = new URL('https://example.com/resources')
location.search = params
console.log({ location })
console.log(location.toString())
// parse
// const query = new URLSearchParams(location.search)
const query = location.searchParams
const q = query.get('q')
console.log({ q })
return q // "bar"
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment