URLSearchParams.has() 带有值polyfill

问题描述 投票:0回答:1

我注意到

value
URLSearchParams.has()
参数仅在 Firefox 中受支持: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/has

示例:

// URL: https://example.com?color=orange

const searchParams = useSearchParams(); // Next.js Hook
const params = new URLSearchParams(searchParams);

// params.has(name, value)
params.has("color", "green") // Firefox correctly returns false; other browsers return true because they only check "color" and not its value.

我正在考虑创建自己的函数,但是已经有一个填充函数可以实现吗?如果是,我该如何使用它(如果有帮助的话我正在使用 Next.js)?

javascript url polyfills urlsearchparams
1个回答
0
投票

这适用于任何支持 searchParams 的浏览器

const hasValue = (params,name,value) => params.has(name) && params.get(name) === value; 
© www.soinside.com 2019 - 2024. All rights reserved.