URLSearchParams & shape URL params
URLSearchParams
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams
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"
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description SearchParamsGenerator
* @augments
* @example
*
* @param {String} search
* @param {Boolean} debug
*
*/
const SearchParamsGenerator = (search = ``, debug = false) => {
let result = {};
// do something...
let params = new URLSearchParams(search);
for (const param of params) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
result[key] = value;
// console.log(`param =`, param);
// param = ["gilcode", "600570.SH"];
// param = ["skin", "white"];
// param = ["name", "恒生电子"];
// param = ["type", "0"];
parseInt("恒生电子", 10);
// NaN
parseInt("0", 10);
// 0
parseInt("11", 2);
// 3
parseInt("11", 10);
// 11
}
return result;
};
let search = decodeURIComponent(window.location.search);
// "?gilcode=600570.SH&skin=white&name=恒生电子&type=0"
SearchParamsGenerator(search);
// {gilcode: "600570.SH", skin: "white", name: "恒生电子", type: "0"}
SearchParamsGenerator(search).gilcode;
// "600570.SH"
// view/stock/index.html?code=600570.SH&skin=white&name=恒生电子&type="0"
// /view/stock/index.html?code=600570.SH&skin=white&name="恒生电子"&type="0"
// decodeURIComponent(window.location.href);
// "f10/view/stock/index.html?code=600570.SH&skin=white&name=恒生电子&type=0"
// decodeURI(window.location.href);
// "/f10/view/stock/index.html?code%3D600570.SH%26skin%3Dwhite%26name%3D恒生电子%26type%3D0"
let searchParams = new URLSearchParams(search);
searchParams.has("code"); // true
searchParams.get("abc"); // null
searchParams.get("code"); // "600570.SH"
searchParams.getAll("code"); // ["600570.SH"]
searchParams.append("xyz", "666");
searchParams.toString();
// "code=600570.SH&skin=white&name=%22%E6%81%92%E7%94%9F%E7%94%B5%E5%AD%90%22&type=%220%22&xyz=666"
decodeURIComponent(searchParams.toString());
// "code=600570.SH&skin=white&name="恒生电子"&type="0"&xyz=666"
searchParams.set("code", "600580.SH");
searchParams.get("code");
// "600580.SH"
searchParams.delete("code");
searchParams.entries();
searchParams.keys();
searchParams.values();
searchParams.sort();
for (const param of params) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
console.log(`param =`, param);
}
for (const param of params.entries()) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
console.log(`param =`, param);
}
for (const param of params.keys()) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
console.log(`param =`, param);
}
for (const param of params.values()) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
console.log(`param =`, param);
}
OK
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description SearchParamsGenerator
* @augments
* @example
*
* @param {String} search
* @param {Boolean} debug
*
*/
const SearchParams = (search = ``, debug = false) => {
const params = new URLSearchParams(search);
let result = {};
for (let param of params) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
result[key] = value;
}
return result;
};
const SearchParamsGenerator = (search = ``, debug = false) => {
const params = new URLSearchParams(search);
let result = {},
entries = [...params.entries()],
keys = [...params.keys()],
values = [...params.values()];
for (let param of params) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
result[key] = value;
}
return {
result,
entries,
keys,
values
};
};
let search = decodeURIComponent(window.location.search);
// "?code=600570.SH&skin=white&name=恒生电子&type=0"
SearchParams(search);
SearchParams(search).code;
// "600570.SH"
SearchParamsGenerator(search);
SearchParamsGenerator(search).result.code;
// "600570.SH"
ES6 modules
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
*
* @description SearchParamsGenerator
* @augments
* @example
*
* @param {String} search
* @param {Boolean} debug
*
*/
const SearchParams = (search = ``, debug = false) => {
const params = new URLSearchParams(search);
let result = {};
for (let param of params) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
result[key] = value;
}
return result;
};
const SearchParamsGenerator = (search = ``, debug = false) => {
const params = new URLSearchParams(search);
let result = {},
entries = [...params.entries()],
keys = [...params.keys()],
values = [...params.values()];
for (let param of params) {
let key = ``,
value = ``;
key = param[0];
value = param[1];
result[key] = value;
}
return {
result,
entries,
keys,
values
};
};
export default SearchParams;
export {
SearchParams,
SearchParamsGenerator
};
/*
let search = decodeURIComponent(window.location.search);
// "?code=600570.SH&skin=white&name=恒生电子&type=0"
SearchParams(search);
SearchParams(search).code;
// "600570.SH"
SearchParamsGenerator(search);
SearchParamsGenerator(search).result.code;
// "600570.SH"
*/
URL
https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams
https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/searchParams
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
URLSearchParams & shape URL params的更多相关文章
- URLSearchParams & Location & URL params parse
URLSearchParams & Location & URL params parse URL params parse node.js env bug node.js & ...
- URLSearchParams 接口
URLSearchParams 接口定义了很多个用来处理 URL 参数串的方法 他可以把对象转变成url上面查询信息的写法,例如a=1&b=2 可以把请求路由中的字符串 key=1 ,拼接 ...
- "奇葩家园“之 asyncTask 与 url 下载篇
asyncTask 是android提供的一个轻量级的异步处理的类,有3个泛型参数,params,progress,result params: 启动任务执行的时候传入的参数比如请求的 url 地址 ...
- 我对Backbone中url属性的理解
Model中有一个url属性,而且有一个urlRoot属性. Collection中也有一个url属性. // 这是Model中的url方法 url: function() { var base = ...
- android 项目中使用到的网络请求框架以及怎样配置好接口URL
我们在做项目中一定少不了网络请求,如今非常多公司的网络请求这块好多都是使用一些比較好的开源框架,我项目中使用的是volley,如今讲讲一些volley主要的使用,假设想要具体的了解就要去看它的源代码了 ...
- Android开发之使用URL訪问网络资源
Android开发之使用URL訪问网络资源 URL (UniformResource Locator)对象代表统一资源定位器,它是指向互联网"资源"的指针. 资源能够是简单的文件或 ...
- java http url post json
import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import ja ...
- JavaScript 功能类 Url.js
简书原文 这个类的主要目的是为了方便平时编码中的Url类型的数据操作 Github 全局名称 全局名称是由源码的最后一行代码确定的,默认为Url,如存在相同名称的对象会抛出异常: 可以通过 requi ...
- 【Python】【BugList13】req = requests.get(url=target)报错: (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)')
[代码] # -*- coding:UTF-8 -*- import requests if __name__ == '__main__': target = 'https://unsplash.co ...
随机推荐
- .NET, NETCORE 怎么写 "超时"代码,解析"超时"代码原理!
干货:本人不会长篇大论.能贴上去的,就是干货,能用一两句话讲明白的,不会大讲概念,不会浪费大家宝贵的时间. 前言:我们发现,超时是个非常重要的概念,如果在通讯架构中,没有超时的设计,那么这个通讯架构就 ...
- Bitter.Core系列九:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore 之 WITH 子句支持
有时我们在聚合查询中,经常会有复杂的聚联查询.有时表的聚联查询SQL 子句比较复杂,DBA 会经常告诉们,能否通过WITH 子句优化.WITH 子句,是对SQL 聚联查询的优化.Bitter.Core ...
- 宝塔Linux命令
安装宝塔 Centos安装脚本 5.7:yum install -y wget && wget -O install.sh http://download.bt.cn/install/ ...
- Communicating sequential processes CSP 通信顺序进程 CSP writing to a file by name (process, Erlang) vs. writing to a file descriptor (channel, Go)
the-way-to-go_ZH_CN/01.2.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...
- 济南学习D1T1_haha
[问题描述] 栈是一种强大的数据结构,它的一种特殊功能是对数组进行排序.例如,借助一个栈,依次将数组1,3,2按顺序入栈或出栈,可对其从大到小排序: 1入栈:3入栈:3出栈:2入栈:2出栈:1出栈. ...
- liux 常用操作命令
tail -f /home/jyapp/apache-tomcat-7.0.59/logs/catalina.out //查看实施日志 //删除临时目录并且启动服务器 rm -rf /home/jy ...
- 使用VMware WorkStation虚拟机软件安装CentOS 8
使用VMware WorkStation虚拟机软件安装CentOS 8 说明:本章我们使用的虚拟机软件是VMware WorkStation15.5 PRO,镜像包是CentOS-8.2.2004-x ...
- docker(6)镜像的使用
前言 Docker的三大核心概念:镜像.容器.仓库.初学者对镜像和容器往往分不清楚,学过面向对象的应该知道类和实例,这跟面向对象里面的概念很相似 我们可以把镜像看作类,把容器看作类实例化后的对象. d ...
- 关于Java注解(annotation)的简单理解
一.什么是注解? 从 JDK5 开始,Java增加对元数据的支持,也就是注解.简单理解就是代码里的特殊标志,这些标志可以在编译,类加载,运行时被读取,并执行相应的处理,以便于其他工具补充信息或者进行部 ...
- CF 1400F x-prime Substrings 题解【AC自动机+DP】
CF 1400F.x-prime Substrings 题意: 给定一个由\('1'\)到\('9'\)组成的字符串\(s\)和一个数\(x\),定义一个串为\(x-prime\)串,当且仅当这个串上 ...