# url-search-params-polyfill

> a simple polyfill for javascript URLSearchParams

Latest version **8.2.5** (published 2023-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install url-search-params-polyfill
pnpm add url-search-params-polyfill
yarn add url-search-params-polyfill
bun add url-search-params-polyfill
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.2.5 |
| Published | 2023-09-05 |
| First published | 2016-09-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 599 |
| Author | Jerry Bendy |
| Maintainers | jerrybendy |
| Keywords | polyfill, URLSearchParams |

## Links

- npm: https://www.npmjs.com/package/url-search-params-polyfill
- Repository: https://github.com/jerrybendy/url-search-params-polyfill
- Issues: https://github.com/jerrybendy/url-search-params-polyfill/issues
- npm.io page: https://npm.io/package/url-search-params-polyfill

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 8.2.5 (latest) — 2023-09-05
- 8.2.4 — 2023-07-05
- 8.2.3 — 2023-06-06
- 8.2.2 — 2023-06-02
- 8.2.1 — 2023-06-02
- 8.2.0 — 2023-06-01
- 8.1.1 — 2021-03-26
- 8.1.0 — 2020-04-24
- 8.0.0 — 2020-02-02
- 7.0.1 — 2019-12-11
- 7.0.0 — 2019-06-30
- 6.0.0 — 2019-04-26
- 5.1.0 — 2019-03-27
- 5.0.1 — 2019-03-25
- 5.0.0 — 2018-09-05
- … 13 more at https://npm.io/package/url-search-params-polyfill/versions

## README

# URLSearchParams Polyfill  [![](https://img.shields.io/npm/v/url-search-params-polyfill.svg)](https://www.npmjs.com/package/url-search-params-polyfill)

This is a polyfill library for JavaScript's `URLSearchParams` class. 

### Features

* Implemented all features from [MDN document](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams).
* Can use for both browsers and Node.js.
* Detect if browsers have full support for `URLSearchParams` and extend it
* Compatible with IE8 and above


## Installation

This can also be installed with `npm`.

```sh
$ npm install url-search-params-polyfill --save
```


For Babel and ES2015+, make sure to import the file:

```javascript
import 'url-search-params-polyfill';
```

For ES5:

```javascript
require('url-search-params-polyfill');
```

For browser, copy the `index.js` file to your project, and add a `script` tag in your html:

```html
<script src="index.js"></script>
```


## Usage

Use `URLSearchParams` directly. You can instantiate a new instance of `URLSearchParams` from a string or an object.

```javascript
// new an empty object
var search1 = new URLSearchParams();

// from a string
var search2 = new URLSearchParams("id=1&from=home");

// from an object
var search3 = new URLSearchParams({ id: 1, from: "home" });

// from location.search, will remove first "?" automatically
var search4 = new URLSearchParams(window.location.search);

// from anther URLSearchParams object
var search5 = new URLSearchParams(search2);

// from a sequence
var search6 = new URLSearchParams([["foo", 1], ["bar", 2]]);
```


### append

```javascript
var search = new URLSearchParams();

search.append("id", 1);
```

### delete

```javascript
search.delete("id");
```

### get

```javascript
search.get("id");
```

### getAll

```javascript
search.getAll("id");
```

### has

```javascript
search.has("id");
```

### set 

```javascript
search.set("id", 2);
```

### toString

```javascript
search.toString();
```

### sort

```javascript
search.sort();
```

### forEach

```javascript
search.forEach(function (item) {
  console.log(item);
});
```

### keys

```javascript
for (var key of search.keys()) {
  console.log(key);
}
```

### values

```javascript
for (var value of search.values()) {
  console.log(value);
}
```

### for...of

```javascript
for (var item of search) {
  console.log('key: ' + item[0] + ', ' + 'value: ' + item[1]);
}
```

### size
```javascript
console.log(search.size)
```

## Known Issues

#### Use with fetch ([#18](https://github.com/jerrybendy/url-search-params-polyfill/issues/18))
Via [fetch spec](https://fetch.spec.whatwg.org/#body-mixin), when passing a `URLSearchParams` object as a request body, the request should add a header with `Content-Type: application/x-www-form-urlencoded; charset=UTF-8`, but browsers which have `fetch` support and not `URLSearchParams` support do not have this behavior.

Via the data of [caniuse](https://caniuse.com/#search=fetch), there are many browsers which support `fetch` but not `URLSearchParams`:

| Edge | Chrome | Opera | Samsung Internet | QQ | Baidu |
| --- | --- | --- | --- | --- | --- |
| 14 - 16 | 40 - 48 | 27 - 35 | 4 | 1.2 | 7.12 |

If you want to be compatible with these browsers, you should add a `Content-Type` header manually:

```js
function myFetch(url, { headers = {}, body }) {
    headers = headers instanceof Headers ? headers : new Headers(headers);
    
    if (body instanceof URLSearchParams) {
        headers.set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    }
    
    fetch(url, {
        headers,
        body
    });
}
```

## LICENSE

MIT license

---
_Source: https://npm.io/package/url-search-params-polyfill · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
