URLSearchParams Polyfill

8.2.5 · active · verified Sun Apr 19

This library provides a comprehensive polyfill for the native JavaScript `URLSearchParams` interface, allowing web developers to utilize its functionality in environments that lack full support, such as older browsers (e.g., IE8+) or specific Node.js versions. The current stable version is 8.2.5. The project appears to have a moderate release cadence, addressing bugs and adding features as needed, with major versions often introducing specific bug fixes or minor behavioral changes rather than massive overhauls. Key differentiators include its broad browser compatibility (down to IE8), full implementation of the MDN specification, and detection of existing `URLSearchParams` implementations to extend them rather than completely overwrite. It works seamlessly in both browser and Node.js environments, ensuring consistent behavior across different JavaScript runtimes.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import the `URLSearchParams` polyfill and use various `URLSearchParams` methods like instantiation from strings or objects, `append`, `set`, `get`, `getAll`, `has`, `delete`, `sort`, and iteration using `for...of`.

import 'url-search-params-polyfill'; // Ensure the polyfill is active

// Instantiate URLSearchParams from various sources
const searchFromString = new URLSearchParams("id=101&from=dashboard&tags=alpha&tags=beta");
console.log('From string:', searchFromString.toString()); // id=101&from=dashboard&tags=alpha&tags=beta

const searchFromObject = new URLSearchParams({ product: 'widget', category: 'electronics' });
searchFromObject.append('color', 'red');
searchFromObject.set('category', 'home-goods');
console.log('From object:', searchFromObject.toString()); // product=widget&category=home-goods&color=red

const queryParams = new URLSearchParams();
queryParams.append("user", "john.doe");
queryParams.append("role", "admin");
queryParams.set("status", "active");
queryParams.append("tags", "new");
queryParams.append("tags", "featured");

console.log("All 'tags' values:", queryParams.getAll("tags")); // ["new", "featured"]
console.log("Has 'user'?", queryParams.has("user")); // true
console.log("Query string:", queryParams.toString()); // user=john.doe&role=admin&status=active&tags=new&tags=featured

queryParams.delete("role");
queryParams.sort();
console.log("Sorted query string after deleting role:", queryParams.toString()); // status=active&tags=featured&tags=new&user=john.doe

for (const [key, value] of queryParams) {
  console.log(`Key: ${key}, Value: ${value}`);
}

view raw JSON →