[英]Why the url first parameter is not being considered in Javascript?
const url = new URLSearchParams('https://example.com?q1=1&q2=2'); console.log(url.has('q3')) // returns false as expected console.log(url.has('q2')) // returns true as expected console.log(url.has('q1')) // returns false as NOT expected
Why it happens?为什么会这样?
The URLSearchParams
constructor, if passed a string, expects that string to be a query string and not a complete URL. URLSearchParams
构造函数,如果传递了一个字符串,则期望该字符串是一个查询字符串,而不是一个完整的 URL。
q1
doesn't appear because your first parameter is https://example.com?q1
. q1
没有出现,因为您的第一个参数是https://example.com?q1
。
const url = new URLSearchParams('https://example.com?q1=1&q2=2'); console.log([...url.entries()]);
Use the URL
constructor if you want to parse a complete URL.如果要解析完整的 URL,请使用
URL
构造函数。
const url = new URL('https://example.com?q1=1&q2=2'); console.log(url.searchParams.has('q3')) console.log(url.searchParams.has('q2')) console.log(url.searchParams.has('q1'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.