当前位置:网站首页>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
边栏推荐
- Caldera安装及简单使用
- multiprocessing. Detailed explanation of pool
- Ora-00909: invalid number of parameters, caused by concat
- POI 替换docx中的文字和图片
- 【毕业季】毕业是人生旅途的新开始,你准备好了吗
- 线程间等待与唤醒机制、单例模式、阻塞队列、定时器
- IDEA中关于Postfix Completion代码模板的一些设置
- 获取地址url中的query参数指定参数方法
- Scala函数柯里化(Currying)
- Information System Project Manager - Chapter VII project cost management
猜你喜欢

Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)

2022 CISP-PTE(一)文件包含

分数阶PID控制

Assembly language - Wang Shuang Chapter 11 flag register - Notes

SQL 注入绕过(一)

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

Currying Scala functions

Information System Project Manager - Chapter VII project cost management

观测电机转速转矩

快速实现单片机和手机蓝牙通信
随机推荐
JVM common instructions
Idea one click log generation
MPC control of aircraft wingtip acceleration and control surface
SQL 注入绕过(一)
[getting started] regular expression Basics
Spark SQL common time functions
Fast implementation of thread mesh networking
Assembly language - Wang Shuang Chapter 11 flag register - Notes
Keep 2 decimal places after multiplying SQLSEVER fields
[QT notes] basic use of qregularexpression in QT
Machine learning
面试官:用分库分表如何做到永不迁移数据和避免热点问题?
C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
论文阅读技巧
POI 替换docx中的文字和图片
飞行器翼尖加速度和控制面的MPC控制
Configuration of vscode korofileheader
【LeetCode】Day90-二叉搜索树中第K小的元素
el-select多个时,el-select筛选选中过的值,第二个el-select中过滤上一个选中的值
快速实现单片机和手机蓝牙通信