Languages
[Edit]
EN

JavaScript - get search parameters from current URL

3 points
Created by:
Frida-Timms
607

In this article, we would like to show you how to get search parameters from the current URL in JavaScript.

Quick solution:

var params = new URLSearchParams(location.search); // e.g. location.search: '?pageNumber=1&pageSize=20'

var pageNumber = params.get('pageNumber');         // 1
var pageSize = params.get('pageSize');             // 20

Notes:

  • URLSearchParams appeared in major web browsers around 2016-2018, and in Node.js 10,
  • read this article to know how to create custom solution that work in all web browsers.

 

Practical example

In this example, we use URLSearchParams to parse location.search value and get query parameter values.

<!doctype html>
<html>
<body>
  <script>

    var params = new URLSearchParams(location.search);

    var pageNumber = params.get('pageNumber');
    var pageSize = params.get('pageSize');

    console.log('pageNumber = ' + pageNumber);
    console.log('pageSize = ' + pageSize);

  </script>
</body>
</html>

Result:

JavaScript - get search parameters from current URL
JavaScript - get search parameters from current URL

See also

  1. JavaScript - get URL request parameters using custom logic

  2. JavaScript - how to get, set, and manage url hash parameters?

  3. JavaScript - get search part from current URL

References

  1. Query string - Wikipedia
  2. URLSearchParams - Web APIs | MDN

Alternative titles

  1. JavaScript - get query parameters from current URL
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join