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


URLSearchParams & Location & URL params parse的更多相关文章

  1. URLSearchParams & shape URL params

    URLSearchParams https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams var paramsString = ...

  2. 关于header('location:url')的一些说明,php缓冲区

    网上搜索header('location:url')的用法,得到如下三个结论: 1. location和“:”号间不能有空格,否则会出错. 2. 在用header前不能有任何的输出. 3. heade ...

  3. Header() in PHP &html – Refresh (Redirect) to Location (URL) in X seconds

    Case 1 : Redirect a page to a URL without waiting in PHP. 1 header("Location: index.php"); ...

  4. Nginx高级应用之Location Url

    基本配置 为了探究nginx的url配置规则,当然需要安装nginx.我使用了vagrant创建了一个虚拟环境的Ubuntu,通过apt-get安装nginx.这样就不会污染mac的软件环境.通过vr ...

  5. Nginx高级应用之Location Url 配置

    原文地址:https://www.linuxidc.com/Linux/2017-03/141910.htm 基本配置 为了探究nginx的url配置规则,当然需要安装nginx.我使用了vagran ...

  6. 简明 Nginx Location Url 配置笔记

    基本配置 为了探究nginx的url配置规则,当然需要安装nginx.我使用了vagrant创建了一个虚拟环境的ubuntu,通过apt-get安装nginx.这样就不会污染mac的软件环境.通过vr ...

  7. location url 反向代理到来机的其它端口 gitlab

    location /nexus { proxy_pass http://127.0.0.1:8081/nexus; } [root@GitMaven conf]# pwd /var/opt/gitla ...

  8. location - URL

    1.hash:获取或设置href 属性中跟在数字符号 # 之后的内容 2.跳转页面: 1)location.href 2)location.replace() 3)location.reload(tr ...

  9. nginx location url解析过程

随机推荐

  1. 跨度实际上是用来计算排位(rank) 目标节点在跳跃表中的排位 有序集 排序计算

    跳跃表的实现 - Redis 设计与实现 http://redisbook.com/preview/skiplist/datastruct.html 有序集合 /* ZSETs use a speci ...

  2.  Go is more about software engineering than programming language research.

    https://talks.golang.org/2012/splash.article Go at Google: Language Design in the Service of Softwar ...

  3. Spring常见问题总结

    1. 什么是 Spring 框架? Spring 是一种轻量级开发框架,旨在提高开发人员的开发效率以及系统的可维护性.Spring 官网:https://spring.io/. 我们一般说 Sprin ...

  4. JAVA中两个int类型的变量在不借助第三个变量的情况下完成值的互换

    在面试中被问到这个问题,想到两种解决方式,在此分享一下. 第一种,使用简单的数学运算达到目标(但是面试官往往会问你还有没有其他方式): public static void main(String[] ...

  5. Spark获取DataFrame中列的方式--col,$,column,apply

    Spark获取DataFrame中列的方式--col,$,column,apply 1.官方说明 2.使用时涉及到的的包 3.Demo 原文作者:大葱拌豆腐 原文地址:Spark获取DataFrame ...

  6. 11月份 chrome 标签整理

    Spring MVC框架相关 Java Web开发 和 linux下开发 汇总 项目源码 优秀的音视频开源框架 常用软件的下载 学习资源或网站 最后分享一些以前收藏的优秀博客 这两天经过3次面试,很幸 ...

  7. Geospark-属性字段处理

    Geospark将从shapefile.csv等格式文件以及DataFrame中的读取的字段保存到了Geometry的userData字段中,可以通过调用.getUserData()方法获取,他会返回 ...

  8. KVM(虚拟机的迁移)

  9. 31-1.解决service iptables save出错

    CentOS 7.x开始,CentOS开始使用systemd服务来代替daemon,原来管理系统启动和管理系统服务的相关命令全部由systemctl命令来代替.service命令只保留下了极少部分使用 ...

  10. shell编程基础一

    1.定义变量 a=1 shell定义变量要注意等号前后不能有空格,不然会报错,请严格按照格式编写. 2.打印输出 echo 1 使用echo打印,后面留一个空格. 3.shell中通过 ${变量名} ...