/

URL & URLSearchParams

javascript
On this page
  • URL
  • URLSearchParams

URL

js
const addr = new URL('https://mysite.com/login?user=someguy&page=news')

URLSearchParams

Docs: developer.mozilla.org/en...ams

js
// example.com?much=wow
const params = new URLSearchParams(window.location.search)
params.has('much') // true
params.get('much') // 'wow'
params.set('such', 'shiny code')
params.toString() // much=wow&such-shiny+code
for ([key, value] of params) {
// ...
}
params.delete('much')
params.toString() // such-shiny+code

Polyfill: github.com/ungap/ur...h-params

Edit this page
Want to make your own site like this?
Try gatsby-theme-code-notes by Zander Martineau.
Notes on code. My second brain, by Zander Martineau.