What is a Query String?
Example of a Simple URL and URL with a Query StringLets say we have a following URL to my site, this is simply the URL of my site:
Now below is the URL to my site that contains query string "name" with a value "farsab". Or we can say it as key, value pairs where the key is "name" and the value is "farsab".
Sometimes we are required to get the values of the query strings contained within the URL string of a webpage. One might want to perform further actions according to the value contained within the query string. A simple example would be that I display an alert if the query string is set or trigger an event or do a anything from a great number of options that we have available.
Considering the above example, here is how the workflow goes:
1) A variable "key" has been declared with a value "name"
2) A variable "value" has been declared which will store the query string value
3) We are calling a function named "getParameterByName" that is defined at the bottom of the script and storing the returned value in the variable named "value"
4) Then I am simply logging the value in the console for now ( here is the place where we can perform other things such as triggering an event or doing anything else )
Multiple Query StringsNow what if we have multiple query strings?, well for multiple query strings, this function might not be efficient enough but still we can simply set the variable for each query string and call the function twice or multiple times as per requirement.
This is the how we can get the values of the query strings and perform further actions based on the returned value.