Parsing URLs and Retrieving Query Parameters in JavaScript
Javascript
2017-03-02 05:50 (8 years ago)

The code new URL(location.href).searchParams.get('q') is a JavaScript expression used to retrieve the value of the query parameter q from the current page's URL. Here is a step-by-step breakdown of what this code does:
location.hrefgets the full URL of the current page.new URL(location.href)creates a new URL object using the current page's URL..searchParamsaccesses the URLSearchParams object associated with the URL, which allows you to work with the query string of the URL..get('q')retrieves the value of the query parameter namedqfrom the URL's query string.
For example, if the current page's URL is https://example.com/page?q=searchterm, executing this code will return the string "searchterm".
Currently unrated
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.
We look forward to discussing your development needs.