8.2.5 • Published 8 months ago

url-search-params-polyfill v8.2.5

Weekly downloads
401,917
License
MIT
Repository
github
Last release
8 months ago

URLSearchParams Polyfill npm.io

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

Features

  • Implemented all features from MDN document.
  • 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.

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

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

import 'url-search-params-polyfill';

For ES5:

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

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

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

Usage

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

// 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

var search = new URLSearchParams();

search.append("id", 1);

delete

search.delete("id");

get

search.get("id");

getAll

search.getAll("id");

has

search.has("id");

set

search.set("id", 2);

toString

search.toString();

sort

search.sort();

forEach

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

keys

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

values

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

for...of

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

size

console.log(search.size)

Known Issues

Use with fetch (#18)

Via fetch spec, 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, there are many browsers which support fetch but not URLSearchParams:

EdgeChromeOperaSamsung InternetQQBaidu
14 - 1640 - 4827 - 3541.27.12

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

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

@sheerid/jslib-nightlybotframework-webchat@axa-ch/pod-kmu-20@piotrowiczmateusz/common-moduleskicad-parserreact-dummy-packagedecoupled-front-endvaluecouponvib-library@ketcher/reactmeiroo-steedos-serverreactivesearchuibrugsdistro@code.gov/code-gov-front-endapollo-componentsconnector-ri@itlangzi/comment-jssid-npm-test3sid-npmtest-1sid-npmtest-2sid-test-create-npmht-pc-loginproduction-enginetipalti-iframe-uiapplication-buyer-dealscocos-kyklarna-mobile-sdk-payments-wrapper-v2hep-examtepgsj@everything-registry/sub-chunk-3023@koreacreditdata/yuhinaopenneuro-apppagetracknumber-running-chimobx-dispatcher-routermodelproxy-ssrminigame-coresdkmhydnhsuk-react-commenting-componentperfect-fetchpkyy-uipixiv.moepolympicspolyfills-betamydacomydaco-femydaco-outer-framemsvtzhny-securitypurplegather-apipod-credit-consumption-ui@devdiary/ui@canopycanopycanopy/b-ber-reader-react@anygraaf/neo-sdk@cdxoo/react-router-url-search-paramsblank-examplebmoor-commblue-box@chrissong/shooks@doodle3d/doodle3d-api@deriv/deriv-chartsbrugssearchui-distro@configuratorware/configurator-admingui@configuratorware/configurator-frontendguiburgeon-r3-ie@considonet/getpostparams@companieshouse/api-sdk-nodebotframework-directlinejsbotframework-directlinejs-rootbotframework-directlinejs-tmpbotframework-webchat-root@dotdev/next-components@clearc2/c2-react-config@compulimtest/do-not-use-botframework-directlinejs@computerrock/react-app-polyfill@cubejs-client/corechium-admin@garyo/vue-gtmvue-running-numbervt-password-pickervue-antu-drone-livevue-antu-drone-lives@twreporter/zoom-in-uiwebgl-benchmark@wundergraph/metro-config@wpbrothers/react-tool@appbaseio/reactivesearch@appbaseio/reactivesearch-vue@arkade/argentzyc-formzengenti-forms-packageapt-maint-react-app@binary-com/smartchartsaxios-mock-server@asteffey/react-fcc-test@axa-ch/pod-rsv@bolt/uikit-workshopbeatchain-artists-react@defichain/ocean-api-client@defichain/whale-api-clientbase-factory
8.2.3

11 months ago

8.2.2

11 months ago

8.2.5

8 months ago

8.2.4

10 months ago

8.2.1

11 months ago

8.2.0

11 months ago

8.1.1

3 years ago

8.1.0

4 years ago

8.0.0

4 years ago

7.0.1

4 years ago

7.0.0

5 years ago

6.0.0

5 years ago

5.1.0

5 years ago

5.0.1

5 years ago

5.0.0

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.0

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

8 years ago

1.0.0

8 years ago