JavaScript URL query string manipulation

Paul Watson
Jun 7, 2021

Changing the URL in the browser with JavaScript has long been a difficult and error prone affair.

You’d try to use regular expressions, split, index, and slice to get at the query string. Then you’d make your change, removal, or addition and then try to use join to put it all back together.

The code was ugly and anything unexpected resulted in an invalid URL.

Thankfully JavaScript and browsers have moved on and URLSearchParams makes working with the query string much easier and less error prone.

--

--

Paul Watson

Web-developer // CTO for OpenUp.org.za // Formerly [CaliberAI, Kinzen, ChangeX, Storyful, FeedHenry] // Learning to code since 1993 // South Africa // EOF