当前位置:网站首页>获取地址url中的query参数指定参数方法
获取地址url中的query参数指定参数方法
2022-06-27 06:30:00 【搬砖的前端】
获取地址url中的query参数指定参数方法
方案一:(兼容性最好,但是有点长)
function getParam(name) {
//先获取#后面的参数
var getValue = function(name, str) {
//获取参数name的值
var reg = new RegExp("(^|!|&|\\?)" + name + "=([^&]*)(&|$)");
//再获取?后面的参数
var r = str.match(reg);
if (r != null) {
try {
return decodeURIComponent(r[2]);
} catch (e) {
console.log(e + "r[2]:" + r[2]);
return null;
}
}
return null;
};
var str = document.location.hash.substr(2);
var value = getValue(name, str);
if (value == null) {
str = document.location.search.substr(1);
value = getValue(name, str);
}
return value;
}
方案二:有兼容性(支持es6 的map就可以)
function getParams(key){
var ops = {
};
window.location.href.split('?')[1].split('&').map(i => ops[(i.split('=')[0])] = i.split('=')[1]);
return ops[key]
}
方案三:有兼容性(支持es6就可以)
- ts不可用,需要改下return那行
- ?. 表示为true就继续往后执行链式
function getParams(key){
var str = window.location.href.split('?')[1].split('&').filter(i => i.startsWith(key+'='));
return str?.[0]?.split('=')[1]
}
基本上主流浏览器都没问题,一般我用万能的第一种,但是有些长
边栏推荐
- 【OpenAirInterface5g】RRC NR解析之RrcSetupComplete
- TiDB的使用限制
- 2018 mathematical modeling competition - special clothing design for high temperature operation
- C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
- Winow10 installation nexus nexus-3.20.1-01
- 技术人员创业一年心得
- tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
- Spark SQL common time functions
- Ora-00909: invalid number of parameters, caused by concat
- 426 binary tree (513. find the value in the lower left corner of the tree, 112. sum of paths, 106. construct a binary tree from the middle order and post order traversal sequence, 654. maximum binary
猜你喜欢
Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
Proxy reflect usage details
Fractional Order PID control
面试官:你天天用 Lombok,说说它什么原理?我竟然答不上来…
JVM类加载机制
Fast implementation of thread mesh networking
[QT] use structure data to generate read / write configuration file code
从5秒优化到1秒,系统飞起来了...
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
多线程带来的的风险——线程安全
随机推荐
解决 Win10 Wsl2 IP 变化问题
一线大厂面试官问:你真的懂电商订单开发吗?
多线程带来的的风险——线程安全
el-select多个时,el-select筛选选中过的值,第二个el-select中过滤上一个选中的值
427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)
Partial function of Scala
路由器和交换机的区别
2022 cisp-pte (I) document contains
2022 le fichier CISP - Pte (i) contient:
TiDB与 MySQL 兼容性对比
Active learning
TiDB的事务概览
Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer
Some settings about postfix completion code template in idea
Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
论文阅读技巧
卷积神经网络---CNN模型的应用(找矿预测)
JVM common instructions
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
2018 mathematical modeling competition - special clothing design for high temperature operation