技术频道


JS 获取 URL 所有参数

在 JavaScript 中,可以通过 URL 对象来解析当前 URL 并获取所有的参数


以下是一个 JS 获取 URL 参数的示例代码:

// 获取当前 URL
const url = new URL(window.location.href);

// 获取所有的搜索参数(即 URL 中的查询字符串部分)
const params = url.searchParams;

// 遍历参数对象,获取从 qst 到 wd 的参数
const result = {};
for (let [key, value] of params) {  
  result[key] = value;
}

console.log(result);
 

CommandNotFound ⚡️ 坑否 - 其他频道扩展阅读:



发表评论