< BACK

Javascript tips, tricks and code snippets

Using URLSearchParams to get a page’s query string

This posting has a good demo discussion of the URLSearchParams API

e.g.

// e.g. ?from=/
var urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.has('from')); // true
console.log(urlParams.get('from')); // "/"
console.log(urlParams.getAll('from')); // ["/"]

outputs

var urlParams = new URLSearchParams(window.location.search); urlParams.has(‘from’) urlParams.getAll(‘from’) urlParams.get(‘from’)