URLSearchParams & Location & URL params parse
URLSearchParams & Location & URL params parse
URL params parse
node.js env bug
node.js & ReferenceError: document is not defined
/*
题目描述
获取 url 中的参数
1. 指定参数名称,返回该参数的值 或者 空字符串
2. 不指定参数名称,返回全部的参数对象 或者 {}
3. 如果存在多个同名参数,则返回数组
示例1
输入 http://www.xgqfrms.xyz?key=1&key=2&key=3&test=4#hehe key
输出 [1, 2, 3]
*/
function getUrlParam(sUrl, sKey) {
const log = console.log;
const a = document.createElement('a');
// node.js & ReferenceError: document is not defined
a.href = sUrl;
// a.href = `http://www.xgqfrms.xyz?key=1&key=2&key=3&test=4#hehe`;
const searchParams = new URLSearchParams(a.search);
if(sKey) {
if(searchParams.has(sKey)) {
const keys = searchParams.getAll(sKey);
return keys.length > 1 ? keys : keys[0];
} else {
return "";
}
} else {
const obj = {};
for (const item of searchParams) {
const [k, v] = item;
obj[k] = v;
}
return obj;
}
}
/*
getUrlParam(`http://www.xgqfrms.xyz?key=1&key=2&key=3&test=4#hehe`, `key`);
// ["1", "2", "3"]
getUrlParam(`http://www.xgqfrms.xyz?key=1&key=2&key=3&test=4#hehe`);
// {key: ["1", "2", "3"], test: ["4"]}
getUrlParam(`http://www.xgqfrms.xyz?key=1&key=2&key=3&test=4#hehe`, `test`);
// "4"
getUrlParam(`http://www.xgqfrms.xyz?key=1&key=2&key=3&test=4#hehe`, `shit`);
// ""
*/
URLSearchParams
The URLSearchParams constructor does not parse full URLs.
var paramsString = "q=URLUtils.searchParams&topic=api";
var searchParams = new URLSearchParams(paramsString);
//Iterate the search parameters.
for (let p of searchParams) {
console.log(p);
}
searchParams.has("topic") === true; // true
searchParams.get("topic") === "api"; // true
searchParams.getAll("topic"); // ["api"]
searchParams.get("foo") === null; // true
searchParams.append("topic", "webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev"
searchParams.set("topic", "More webdev");
searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev"
searchParams.delete("topic");
searchParams.toString(); // "q=URLUtils.searchParams"
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
Location
query string
// Create anchor element and use href property for the purpose of this example
// A more correct alternative is to browse to the URL and use document.location or window.location
const al = document.createElement('a');
a.href = 'https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container';
// query string
console.log(a.search); // ?q=URL
console.log(a.href); // https://developer.mozilla.org:8080/en-US/search?q=URL#search-results-close-container
console.log(a.protocol); // https:
console.log(al.host); // developer.mozilla.org:8080
console.log(a.hostname); // developer.mozilla.org
console.log(a.port); // 8080
console.log(a.pathname); // /en-US/search
console.log(a.hash); // #search-results-close-container
console.log(al.origin); // https://developer.mozilla.org:8080
https://developer.mozilla.org/en-US/docs/Web/API/Location
https://developer.mozilla.org/en-US/docs/Web/API/Window/location
https://developer.mozilla.org/en-US/docs/Web/API/Document/location
demo
function getUrlParam(sUrl, sKey) {
const a = document.createElement('a');
a.href = `http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe`;
}
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13540392.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2019-08-21 js & touch event & swipe
2019-08-21 EventEmitter & custom events
2019-08-21 ES6 Class & super All In One
2019-08-21 Typescript All In One
2019-08-21 promise & async
2019-08-21 ios clipboard duplicate bug
2019-08-21 GitHub & two-factor authentication