Published in


Parsing URL Query String

A definite guide through one of the most common & asked web development operation

Query String is one of the most classical and used ways to share data between environments and applications.

Historically used as a single-line text placed inside the <isindex> HTML element and sent to the server as a query string addition to the GET request URL, so the web server could respond with a list of pages that matched the keywords.

With the arrival of Web Forms, query strings was then recycled to contain the form fields (name + values) and thus, gained the shape we know today:

  • Key-value pair based.
  • Within each pair, the key and value are separated by an equal sign =.
  • The key-value sequences are separated by the ampersand & or semicolon sign ;.
  • Some chars cannot be part of the URL and therefore, must be encoded through its UTF-8 representation. E.g. #, SPACE, so on.

Since then things are pretty much the same, with exception of its use cases, that have grown.

Before we get more depth on the subject, let’s take a look on the URL anatomy to really understand what are we dealing with.

URL Anatomy

Node.js URL anatomy overview

URL Encoding

For this process, there are 2 methods available in the Browser’s API:

  • encodeURI()
  • encodeURIComponent()

encodeURI vs encodeURIComponent

Both methods encodes a URI by replacing the some especial chars with its UTF-8 representation.

The difference is the character exception list of each method and given URI assumption.


Assumes that the given URI is complete and will not encode anything between URI protocol and host.

Exception listA-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #


Will encode every char that is not in the exception list.

Exception listA-Z a-z 0-9 - _ . ! ~ * ' ( )


// outputs ""
// outputs ""

Accessing URL Query String

Retrieving the URL Query String is a fairly simple operation, it is available under the search property from the window.location API.


Assuming that you’re under the following URL, you will get:
// outputs "?search=foo&page=1&sortBy=desc"

Alright! We got all the foundation clear, so it’s time to go straight to the point.






Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store