Kodeclik Blog


Getting URL parameters with Javascript

Most (large) websites allow you to not only access webpages but also support URL parameters, which is a way to add extra information to a website's URL. They are like tags that help the website understand what you want to see or do. The information in the tag tells the website that you want it to search for particular pages or retrieve specific information.
URL parameters are added to the end of a URL after a question mark (?). They are made up of key-value pairs, separated by an equals sign (=). For instance if you are using an library web site, it might support URL parameters such as “book=HarryPotter”. (This is an example, which URL parameters a site supports is dependent on the site.) For instance, if the website URL is “https://mylibrary.com”, then the full URL with URL parameters will be: “https://mylibrary.com?book=HarryPotter”. Some websites allow multiple parameters which are usually separated by an ampersand (&), e.g., “https://mylibrary.com?book=HarryPotter&year=2010”.
URL parameters can be used to filter and organize content on a website, or to pass information between pages. They can also be used for tracking, to see how many people are visiting a website or clicking on a link.
An example of passing URL parameters to a real website can be seen in Wikipedia. We can construct the following URL: https://en.wikipedia.org/w/index.php?title=Sun where the parameter key is “title” and the value is “Sun”. If you type this URL into a browser, you will retrieve the Wikipedia webpage for Sun, like so:
Now, if you would like to process such URLs inside your Javascript program and extract out those key and value pairs, you can write code such as:
In the above code, we are using the URL interface which returns a URLSearchParams object that allows access to the GET decoded query arguments contained in the URL. The second line extracts the parameter called “title” and then prints its value to the console. The output will be:
Another way to use this functionality is in the code below:
Here we are using window.location.search to access the current URL and constructing a URLSearchParams object with it. Then we are using get to extract the value associated with the “myParam” key. To illustrate this code with a real site, if the current URL is "https://www.example.com/?myParam=hello" the above code will log "hello" to the console. This is often useful if you need to analyze the current URL and do something different depending on the URL parameters.
URL parameters are very often used with websites that expose an API for public usage. For instance, consider the National Weather Service (NWS) API which allows developers to access critical forecasts, alerts, and observations, along with other weather data. The API is based on JSON-LD and is designed with a cache-friendly approach that expires content based on the information life cycle. To use the NWS API, you can use the following URL with the appropriate parameters:
Here, "latitude" and "longitude" are variables denoting the latitude and longitude coordinates of the location you want to get weather data for. For example, to get weather data for Washington, DC you could use the following URL:
because (38.8898, -77.0091) are the latitude and longitudes of Washington DC. This particular URL (because it is an API) returns a JSON object, so you will see this on your browser:
As you can see the returned JSON clearly labels the result as Washington DC. But note that in this case, the URL does not follow the key=value convention. Because it is a formal API, it has a specific convention to call it (which again we need to be aware of).
In summary, URL parameters are a way to add extra information to a website's URL, which can be used to filter and organize content, send some small details from the client (browser) to the server, pass information between pages, or track user activity. They can contain all kinds of useful information, such as search queries, catalog details, product categories, and more.
If you liked this blogpost, learn how to download a file using Javascript.
Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2024. All rights reserved.