Samples for URLSearchParams

Querystring API in Javascript.

CRUD Querystring Params

The URLSearchParams API allows us to take values from any string and amend, delete and add to it. View the source of this page to see the code.



                        var ipUrl = $("input#qs1").val();
                        const params = new URLSearchParams(ipUrl);
                        params.set('page', 3);
                        params.delete('moo');
                        $("span#op1").html(params.toString());
                    

Output:

Working with current page URL and Querystring

It comes in handy with working with window.location and window.location.search. Click the button and watch the querystring



                        var ipUrl = window.location;
                        const params = new URLSearchParams(ipUrl.search);
                        params.set('foo', 123);
                        params.set('bar', 'abc');
                        window.location.search = params.toString();