// Imagine the following URL: https://codepad.co/snippet?foo=1&bar=beer&you=great
let url = new URL(window.location.href);
let params = new URLSearchParams(url.search.slice(1));
// Log the values of each parameter
for (let p of params) {
console.log(p);
}
// The main functions you can call on params
params.has('foo'); // returns true
params.get('foo'); // returns 1
params.set('you', 'awesome');
params.delete('bar');
params.toString(); // returns 'foo=1&you=awesome'
// Testing and polyfill
if('URLSearchParams' in window) {
console.log('URLSearchParams is supported!');
} else {
// Go to github.com/WebReflection/url-search-params for a polyfill.
}
URLSearchParams as implemented by Chrome 49, Firefox 44, and Opera 36.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.