CyrefJS
CCyrefJS
فا
Back to documentation
URL/Since 1.38.0

removeQueryParam

Removes a specific query parameter from a URL or query string while preserving the hash fragment.

removeQueryParam(url, key):string

Hash Preservation

The function separates the hash fragment before processing the query and appends it to the returned result unchanged.

Query Processing

The query portion is parsed with URLSearchParams and the specified key is removed using delete().

URL and Query String Support

The function handles full URL-like strings as well as query strings. When there is no question mark and the input does not contain a slash, the input is treated directly as query data.

Empty Query

When removing the final query parameter, the question mark is removed instead of leaving an empty query string. The hash, if present, remains.

Duplicate Keys

All values associated with the specified key are removed by URLSearchParams.delete().

Parameters

urlstringrequired

The URL, query string, or URL-like string from which the parameter should be removed.

keystringrequired

The query parameter name to remove.

Returns

string

Returns the URL or query string with the specified parameter removed and the hash fragment preserved.

Examples

01

Remove a parameter from a URL

Remove a query parameter while keeping the rest of the URL unchanged.

TypeScript
import { removeQueryParam } from "@cyref/js";

removeQueryParam(
  "https://example.com/products?page=2&sort=latest",
  "page",
);
// "https://example.com/products?sort=latest"
02

Remove the only parameter

When the removed parameter is the only query parameter, the question mark is removed as well.

TypeScript
removeQueryParam(
  "https://example.com/products?page=2",
  "page",
);
// "https://example.com/products"
03

Preserve hash

The hash fragment is preserved when removing a query parameter.

TypeScript
removeQueryParam(
  "https://example.com/products?page=2#details",
  "page",
);
// "https://example.com/products#details"
04

Query string input

A query string without a URL path can also be processed.

TypeScript
removeQueryParam("?page=2&sort=latest", "page");
// "?sort=latest"

removeQueryParam("?page=2", "page");
// ""
05

Repeated parameters

All occurrences of the specified key are removed because URLSearchParams.delete() removes every value associated with that key.

TypeScript
removeQueryParam(
  "https://example.com?tag=js&tag=ts&sort=latest",
  "tag",
);
// "https://example.com?sort=latest"
Built with@cyref/js·Official documentation