Get URL Parameters with JavaScript

URL Parameters (also known as Query Parameters or Query String) are a set o key value pars attached to the end of a URL. They are used to send small amounts of data from page to page, or from client to server via the URL.

TL;DR

const que…

URL Parameters (also known as Query Parameters or Query String) are a set o key value pars attached to the end of a URL. They are used to send small amounts of data from page to page, or from client to server via the URL.

TL;DR

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);

// https://example.com/path/to/page?color=purple&size=M&size=L

urlParams.get('color')     // purple
urlParams.getAll('size')   // ['M', 'L']

URL Parameters Structure

The query parameters are separate from URL path with a ? (question mark):

https://example.com/path/to/page?color=purple

Each parameter after the first one is joined with an & (ampersand):

https://example.com/path/to/page?color=purple&size=M&size=L

In this case the query string is color=purple.

There are caracters that cannot be part of a URL (for example space) and some other characters have a special meaning in a URL (the character #). This types of characters need to be encoded (for example space is encoded as %20).

Get a URL parameter

You can get the query string using window.location.search.

const queryString = window.location.search;

You can then parse the query string’s parameters using URLSearchParams:

const urlParams = new URLSearchParams(queryString);

Now you can use URLSearchParams.get() to get the first value associated with the given search parameter:

// https://example.com/path/to/page?color=purple&size=M&size=L

urlParams.get('color')   // purple
urlParams.get('size')    // M
urlParams.get('nothing') // empty string

Get all values of a URL parameter

Now you can use URLSearchParams.getAll() to get all values associated with the given search parameter:

// https://example.com/path/to/page?color=purple&size=M&size=L

urlParams.getAll('color') // ['purple']
urlParams.getAll('size')  // ['M', 'L']

Check if a URL parameter exists

You can use URLSearchParams.has() to check if a given parameter exists.

// https://example.com/path/to/page?color=purple&size=M&size=L

urlParams.has('color')   // true
urlParams.has('size')    // true
urlParams.has('nothing') // false

Iterating over URL parameters

Iterate through all keys:

const keys = urlParams.keys();

for (const key of keys) {
  console.log(key);
} 

Iterate through all values:

const values = urlParams.values();

for (const value of values) {
  console.log(value);
}

Iterate through all key/value pairs:

const entries = urlParams.entries();

for(const entry of entries) {
  console.log(`${entry[0]} = ${entry[1]}`);
}

For Internet Explorer

The URLSearchParams is not suported on IE, so you will need to parse the URL and get the query parameters.

function getParameterByName(name) {
  cont url = window.location.search;
  name = name.replace(/[\[\]]/g, '\\$&');

  let regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
  let results = regex.exec(url);

  if (!results) {
    return null;
  } 

  if (!results[2]) {
    return '';
  }

  return decodeURIComponent(results[2]);
}

Now you can use getParameterByName to get the first value associated with the given search parameter:

// https://example.com/path/to/page?color=purple&size=M&size=L

getParameterByName('color')   // purple
getParameterByName('size')    // M
getParameterByName('nothing') // null

Print Share Comment Cite Upload Translate
APA
Dezmerean Robert | Sciencx (2024-03-29T11:28:27+00:00) » Get URL Parameters with JavaScript. Retrieved from https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/.
MLA
" » Get URL Parameters with JavaScript." Dezmerean Robert | Sciencx - Monday June 27, 2022, https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/
HARVARD
Dezmerean Robert | Sciencx Monday June 27, 2022 » Get URL Parameters with JavaScript., viewed 2024-03-29T11:28:27+00:00,<https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/>
VANCOUVER
Dezmerean Robert | Sciencx - » Get URL Parameters with JavaScript. [Internet]. [Accessed 2024-03-29T11:28:27+00:00]. Available from: https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/
CHICAGO
" » Get URL Parameters with JavaScript." Dezmerean Robert | Sciencx - Accessed 2024-03-29T11:28:27+00:00. https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/
IEEE
" » Get URL Parameters with JavaScript." Dezmerean Robert | Sciencx [Online]. Available: https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/. [Accessed: 2024-03-29T11:28:27+00:00]
rf:citation
» Get URL Parameters with JavaScript | Dezmerean Robert | Sciencx | https://www.scien.cx/2022/06/27/get-url-parameters-with-javascript/ | 2024-03-29T11:28:27+00:00
https://github.com/addpipe/simple-recorderjs-demo