Get query string value JavaScript

To get a query string value from a URL in JavaScript, you can use the following steps:

  1. Get the URL of the current page using window.location.href.
  2. Parse the URL to extract the query string using new URL().
  3. Find the value of the query string parameter you are interested in using the searchParams property.

See the Pen Untitled by Nirbhay (@nirbhaysingh) on CodePen.

In this example, the URL constructor is used to create a new URL object from the given url. The searchParams property of the URL object is then used to get the value of the fname and lname parameter in the query string. If the fname or lname parameter is not present in the query string, then value will be null

No comments :

Please Give Your Feedback