JavaScript获取get参数

URL 接口的 searchParams 属性返回一个 URLSearchParams 对象,这个对象包含当前 URL 中解码后的 GET 查询参数。

如果你的 URL 是 https://example.com/?name=Jonathan%20Smith&age=18 ,你可以这样解析 URL,然后得到 name 和 age 的值。

let params = (new URL(document.location)).searchParams;
let name = params.get('name'); // is the string "Jonathan Smith".
let age = parseInt(params.get('age')); // is the number 18

  

posted @ 2022-01-12 19:25  洛飞  阅读(446)  评论(0编辑  收藏  举报