当前位置:网站首页>Get the query parameter in the address URL specify the parameter method
Get the query parameter in the address URL specify the parameter method
2022-06-27 06:48:00 【Front end of brick moving】
Get address url Medium query Parameterspecify the parameter method
Scheme 1 :( Best compatibility , But it's a little long )
function getParam(name) {
// First get # Later parameters
var getValue = function(name, str) {
// To obtain parameters name Value
var reg = new RegExp("(^|!|&|\\?)" + name + "=([^&]*)(&|$)");
// Get more ? Later parameters
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;
}
Option two : Compatibility ( Support es6 Of map Can )
function getParams(key){
var ops = {
};
window.location.href.split('?')[1].split('&').map(i => ops[(i.split('=')[0])] = i.split('=')[1]);
return ops[key]
}
Option three : Compatibility ( Support es6 Can )
- ts Unavailable , Need to change return That line
- ?. Expressed as true Just continue to follow the chain
function getParams(key){
var str = window.location.href.split('?')[1].split('&').filter(i => i.startsWith(key+'='));
return str?.[0]?.split('=')[1]
}
Basically, mainstream browsers have no problem , Generally, I use the first kind of omnipotent , But it's a little long
边栏推荐
- 面试官:用分库分表如何做到永不迁移数据和避免热点问题?
- Write an example of goroutine and practice Chan at the same time
- 高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
- Visual Studio VS 快捷键使用大全
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
- Fast implementation of thread mesh networking
- 网关状态检测 echo request/reply
- LeetCode 0086. Separate linked list
- winow10安装Nexus nexus-3.20.1-01
- 主动学习(active learning)
猜你喜欢
随机推荐
TiDB 基本功能
poi导出excle
SQL 注入绕过(一)
IDEA中关于Postfix Completion代码模板的一些设置
Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)
Gaussian distribution, linear regression, logistic regression
[QT] use structure data to generate read / write configuration file code
Unrecognized VM option ‘‘
Idea one click log generation
快速实现Thread Mesh组网详解
Scala advanced_ Member access modifier
Fast realization of Bluetooth communication between MCU and mobile phone
View functions in tidb
Meaning of 0.0.0.0:x
Information System Project Manager - Chapter VII project cost management
[QT notes] basic use of qregularexpression in QT
Mathematical modeling contest for graduate students - optimal application of UAV in rescue and disaster relief
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
POI 替换docx中的文字和图片
2022 CISP-PTE(二)SQL注入









