JS URLs

Parse location.hash or location.search variables:

    location.hash = "foo=1&bar=2"

    // parse the hash
    let params = new URLSearchParams(window.location.hash.substr(1))
    console.assert(x.get("foo") === "1")

    // change a variable
    params.set("bar", 3)

    // convert to a url-sutable string
    console.assert(params.toString() === "foo=1&bar=3"

Add search params:

    var url = new URL("/foo", location.href)

    // add a parameter
    url.searchParams.append("foo", "bar")

    // set a parameter, if there are several values, the others are deleted
    url.searchParams.set("foo", "bar")

    // make as string of params sutable for url
    url.searchParams.toString()

Insatall polyfill: npm install js-polyfills

(function () {
    try {
        if (window.URL) {
            var nativeURL = new URL("http://example.com");
            if ("searchParams" in nativeURL && "href" in nativeURL) return;
        }
    } catch (_) {}
    console.log("polyfill: URL");
    document.write('<script src="/node_modules/js-polyfills/url.js"><\/script>');
})();