当前位置:网站首页>获取地址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]
}
基本上主流浏览器都没问题,一般我用万能的第一种,但是有些长
边栏推荐
- Ora-00909: invalid number of parameters, caused by concat
- HTAP 快速上手指南
- One year's experience of technical personnel in Entrepreneurship
- 面试官:你天天用 Lombok,说说它什么原理?我竟然答不上来…
- Scala之偏函数Partial Function
- JVM tuning ideas
- el-select多个时,el-select筛选选中过的值,第二个el-select中过滤上一个选中的值
- [QT notes] basic use of qregularexpression in QT
- Quick realization of Bluetooth ibeacn function
- HTAP 深入探索指南
猜你喜欢

Ahb2apb bridge design (2) -- Introduction to synchronous bridge design

研究生数学建模竞赛-无人机在抢险救灾中的优化应用

SQL injection bypass (I)

Redis cache penetration, cache breakdown, cache avalanche

Fast realization of Bluetooth communication between MCU and mobile phone

Dev++ 环境设置C语言关键字显示颜色

Us camera cloud service scheme: designed for lightweight video production scenes

网关状态检测 echo request/reply

thrift

LeetCode 0086. Separate linked list
随机推荐
Active learning
Assembly language - Wang Shuang Chapter 8 two basic problems in data processing - Notes
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
JVM class loading mechanism
JVM object composition and storage
TiDB 中的数据库模式概述
Instance Tunnel 使用
G1和ZGC垃圾收集器
Force buckle 179, max
观测电机转速转矩
[cultivation system] common regular expressions
POI 替换docx中的文字和图片
Ora-00909: invalid number of parameters, caused by concat
Convolution neural network -- Application of CNN model (ore prospecting prediction)
426-二叉树(513.找树左下角的值、112. 路径总和、106.从中序与后序遍历序列构造二叉树、654. 最大二叉树)
Spark SQL common time functions
Mathematical modeling contest for graduate students - optimal application of UAV in rescue and disaster relief
On gpu: historical development and structure
快速实现单片机和手机蓝牙通信
Information System Project Manager - Chapter VII project cost management