xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js 解析 URL query 参数 All In One

js 解析 URL query 参数 All In One

const url = new URL('https://www.xgqfrms.xyz/index.html?date=2022-03-20');
const searchParams = new URLSearchParams(url.search);

for (let param of url.searchParams) {
    console.log('param =', param);
}
// param = (2) ['date', '2022-03-20']

demo

const url = new URL('https://www.xgqfrms.xyz/index.html?date=2022-03-20');

url;
/*
URL {origin: 'https://www.xgqfrms.xyz', protocol: 'https:', username: '', password: '', host: 'www.xgqfrms.xyz', …}hash: ""host: "www.xgqfrms.xyz"hostname: "www.xgqfrms.xyz"href: "https://www.xgqfrms.xyz/index.html?date=2022-03-20"origin: "https://www.xgqfrms.xyz"password: ""pathname: "/index.html"port: ""protocol: "https:"search: "?date=2022-03-20"searchParams: URLSearchParams[[Prototype]]: URLSearchParamsappend: ƒ append()delete: ƒ delete()entries: ƒ entries()forEach: ƒ forEach()get: ƒ ()getAll: ƒ getAll()has: ƒ has()keys: ƒ keys()set: ƒ ()sort: ƒ sort()toString: ƒ toString()values: ƒ values()constructor: ƒ URLSearchParams()Symbol(Symbol.iterator): ƒ entries()Symbol(Symbol.toStringTag): "URLSearchParams"[[Prototype]]: Objectusername: ""[[Prototype]]: URL
*/

url.searchParams;
// URLSearchParams {}

url.searchParams.entries();
// Iterator {}[[Prototype]]: Iterator
for (let param of url.searchParams) {
    console.log('param =', param);
}
// param = (2) ['date', '2022-03-20']



API

https://developer.mozilla.org/zh-CN/docs/Web/API/Location/search

URLSearchParams

let params = new URLSearchParams(document.location.search);
let name = params.get("name"); 
// is the string "eric"
let age = parseInt(params.get("age"), 10); 
// is the number 18

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get#examples

searchParams

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

https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams#examples

refs

https://www.cnblogs.com/xgqfrms/p/16031416.html



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(943)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-04-09 calendar merge date
2020-04-09 js 获取是否为闰年,以及各月的天数 & isLeapYear
2020-04-09 scrollTo & js auto scroll & scrollX & scrollY
2019-04-09 email 聚合 & 批量删除
2019-04-09 windows 10 & 禁用服务.bat
2019-04-09 vue render img
2016-04-09 Essential Sublime Text 3 Plugins for PHP Developers, Sublime Text (3) for PHP Developers
点击右上角即可分享
微信分享提示