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

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

https://www.nowcoder.com/practice/a3ded747e3884a3c86d09d88d1652e10?tpId=2&&tqId=10852&rp=1&ru=/ta/front-end&qru=/ta/front-end/question-ranking

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 发布文章使用:只允许注册用户才可以访问!


posted @   xgqfrms  阅读(219)  评论(8编辑  收藏  举报
编辑推荐:
· 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
点击右上角即可分享
微信分享提示