Often in Single Page Applications, you will have to interact with the URL to implement some functionality like pagination or filtering.

The URL is a great place to put some of your applications state on it. The advantages are that users can copy the URL and share it and getting back to it with the set filters and page where they left off.

The hard part is, how you get and update those URL parameters. Frankly, in modern browsers, there is a neat API named URLSearchParams. It allows us to extremely easy to retrieve or update the URL parameters. URLSearchParams is available in node.js and most browsers.

Recently we had an issue with an npm package named query-string in production which would throw an error in IE11, so we decided to remove it and use URLSearchParams with a polyfill instead. We could have switched to the older version 5 and support older browsers, but I wanted to try out something new.

query-string is a great library and you can still use it, but it is more focused to be used on the backend with node.js and for newer browsers.

#javascript #web-development #browsers #javascript-tips

Dealing with URL query parameters in Javascript using URLSearchParams
3.00 GEEK