Created
July 27, 2018 20:36
-
-
Save hubgit/b4659c847970de48a934f3c42448a611 to your computer and use it in GitHub Desktop.
URLSearchParams
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
(() => { | |
// 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