Url Search Params Polyfill

a simple polyfill for javascript URLSearchParams
Alternatives To Url Search Params Polyfill
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Fetch25,788250,5919,1752 days ago37September 11, 20234mitJavaScript
A window.fetch JavaScript polyfill.
Isomorphic Fetch6,936185,2067,8879 months ago23September 23, 202054mitJavaScript
Isomorphic WHATWG Fetch API, for Node & Browserify
Unfetch5,5602,4671,5707 months ago10January 03, 202323mitJavaScript
🐕 Bare minimum 500b fetch polyfill.
Form To Google Sheets3,719
4 months ago53apache-2.0JavaScript
Store HTML form submissions in Google Sheets.
Cross Fetch1,58714,3085,4924 months ago49July 03, 202310mitJavaScript
Universal WHATWG Fetch API for Node, Browsers and React Native.
React Ie898825 years ago7January 06, 201621
Make your React app work in IE8
Fetch Jsonp9811,4009935 months ago19July 06, 2023JavaScript
Make JSONP request like window.fetch
Svgxuse74417371a year ago26October 25, 201713mitJavaScript
A simple polyfill that fetches external SVGs referenced in use elements when the browser itself fails to do so. Demo: https://icomoon.io/svgxuse-demo/
Url Search Params Polyfill5865403843 months ago28September 05, 20235mitJavaScript
a simple polyfill for javascript URLSearchParams
Abortcontroller Polyfill3131,4083949 months ago40October 08, 202212mitJavaScript
Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
Alternatives To Url Search Params Polyfill
Select To Compare


Alternative Project Comparisons
Readme

URLSearchParams Polyfill

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:

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:

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

Popular Polyfill Projects
Popular Fetch Projects
Popular Libraries Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Javascript
Fetch
Polyfill
Es5