简体   繁体   English

为什么Javascript中没有考虑第一个参数url?

[英]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.

相关问题 为什么在此 Javascript 代码中不将“0”和“未定义”视为错误? - Why '0' and 'undefined' are not being considered as false in this Javascript code? JavaScript 循环中不考虑 Google-Sheet 的第一行 - First row of Google-Sheet is not being considered in JavaScript Loop 为什么首先考虑上述else语句而不是上面的if值之一? - Why is this else statement being considered first than one of the if values above? 为什么我的javascript不被认为是安全的? - why is my javascript not considered secure? 为什么javascript:void(0)被认为有害? - Why is javascript:void(0) considered harmful? 为什么解析参数首先出现在 JavaScript Promises 中? - Why resolve parameter comes first in JavaScript Promises? JavaScript:如何从 URL 获取第一个参数,实际上是第一个键? - JavaScript: How to get first parameter, actually first key from URL? 为什么不将“function”视为javascript中的数据类型? - why isn't “function” considered a datatype in javascript? 为什么JavaScript中的函数既是构造函数又是对象? - Why in JavaScript is a function considered both a constructor and an object? 为什么集合被认为是JavaScript中的键控集合? - Why Sets are considered keyed collections in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM