当前位置:网站首页>【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'
边栏推荐
- Chapter 7 - class foundation
- Kt148a voice chip IC user end self replacement voice method, upper computer
- Overview of browser caching mechanism
- Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
- Golang concurrent programming goroutine, channel, sync
- 函数高阶-柯里化实现
- 使用IDM下载百度网盘的文件(亲测有用)[通俗易懂]
- HDL design peripheral tools to reduce errors and help you take off!
- Py之interpret:interpret的简介、安装、案例应用之详细攻略
- Génération automatique de fichiers d'annotation d'images vgg
猜你喜欢
Refactoring: improving the design of existing code (Part 1)
Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
Notes on hardware design of kt148a voice chip IC
良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!
Kt148a voice chip IC software reference code c language, first-line serial port
RPD product: super power squad nanny strategy
How to avoid duplicate data in gaobingfa?
定了,就是它!
随机推荐
AcWing 1128. Messenger solution (shortest path Floyd)
定了,就是它!
JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
450 Shenxin Mianjing 1
蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
Use IDM to download Baidu online disk files (useful for personal testing) [easy to understand]
Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
AcWing 341. 最优贸易 题解 (最短路、dp)
CheckListBox control usage summary
多态的理解以及作用
R language uses econcharts package to create microeconomic or macroeconomic maps, and indifference function to visualize indifference curve
Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
Notes de lecture sur le code propre
思考变量引起的巨大变化
AcWing 343. Sorting problem solution (Floyd property realizes transitive closure)
MySQL function
PXE installation "recommended collection"
编写完10万行代码,我发了篇长文吐槽Rust
452-strcpy、strcat、strcmp、strstr、strchr的实现