当前位置:网站首页>【JS】获取hash模式下URL的搜索参数
【JS】获取hash模式下URL的搜索参数
2022-07-02 18:54:00 【一颗不甘坠落的流星】
文章目录
- 函数封装:
function getHashSearchParam(key) {
const url = location.href
// 获取 hash 值,不包含 '#' 号
const hash = url.substring(url.indexOf("#") + 1)
// 查找 '?' 号所在的索引
const searchIndex = hash.indexOf("?")
// '?' 号后面接的是索引参数,如果找到则+1,去除'?' 号
const search = searchIndex !== -1 ? hash.substring(searchIndex + 1) : ""
// 把搜索参数字符串提过URLSearchParams转成对象形式
const usp = new URLSearchParams(search)
// 通过URLSearchParams自带的get方法,查询键所对应的值
return usp.get(key)
}
- 使用案例:
// 假设当前页面url为
https://www.test.com/#/aa/bb?project_id=666
const project_id = getHashSearchParam("project_id")
console.log(project_id) // '666'
边栏推荐
- Detailed tutorial on installing stand-alone redis
- 基于SSM实现网上购物商城系统
- Is there any security guarantee for the ranking of stock and securities companies
- SQLite 3.39.0 release supports right external connection and all external connection
- Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
- 从20s优化到500ms,我用了这三招
- upload-labs
- Reading notes of "the way to clean structure" (Part 2)
- 多端小程序开发有什么好处?覆盖百度小程序抖音小程序微信小程序开发,抢占多平台流量红利
- 蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
猜你喜欢
随机推荐
Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
Think about the huge changes caused by variables
Build a master-slave mode cluster redis
B端电商-订单逆向流程
Set up sentinel mode. Reids and redis leave the sentinel cluster from the node
自动生成VGG图像注释文件
How to set priorities in C language? Elaborate on C language priorities
AcWing 383. Sightseeing problem solution (shortest circuit)
Detailed explanation of VBScript (I)
KT148A语音芯片ic的开发常见问题以及描述
Solution: vs2017 cannot open the source file stdio h main. H header document [easy to understand]
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
AcWing 1137. 选择最佳线路 题解(最短路)
JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
自動生成VGG圖像注釋文件
中缀表达式转换为后缀表达式(C语言代码+详解)
KT148A语音芯片ic的硬件设计注意事项
ShardingSphere-JDBC5.1.2版本关于SELECT LAST_INSERT_ID()本人发现还是存在路由问题
Zabbix5 client installation and configuration