当前位置:网站首页>【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'
边栏推荐
- LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
- 中缀表达式转换为后缀表达式(C语言代码+详解)
- 450-深信服面经1
- AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
- Golang concurrent programming goroutine, channel, sync
- Function high order curry realization
- Chapter 7 - class foundation
- AcWing 342. Road and route problem solving (shortest path, topological sorting)
- 451-memcpy、memmove、memset的实现
- JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
猜你喜欢

GCC: Graph Contrastive Coding for Graph Neural NetworkPre-Training

AcWing 342. Road and route problem solving (shortest path, topological sorting)

字典

Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1

API文档工具knife4j使用详解

Overview of browser caching mechanism

Embedded (PLD) series, epf10k50rc240-3n programmable logic device

zabbix5客户端安装和配置

蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么

One side is volume, the other side is layoff. There are a lot of layoffs in byte commercialization department. What do you think of this wave?
随机推荐
Start practicing calligraphy
Use IDM to download Baidu online disk files (useful for personal testing) [easy to understand]
Reading notes of code neatness
AcWing 181. Turnaround game solution (search ida* search)
Codeworks round 802 (Div. 2) pure supplementary questions
【NLP】一文详解生成式文本摘要经典论文Pointer-Generator
Yes, that's it!
自动生成VGG图像注释文件
Overview of browser caching mechanism
Common problems and description of kt148a voice chip IC development
451 implementation of memcpy, memmove and memset
AcWing 1137. 选择最佳线路 题解(最短路)
AcWing 1125. 牛的旅行 题解(最短路、直径)
KT148A语音芯片ic的软件参考代码C语言,一线串口
Introduction to program ape (XII) -- data storage
451-memcpy、memmove、memset的实现
编写完10万行代码,我发了篇长文吐槽Rust
励志!大凉山小伙全奖直博!论文致谢看哭网友
Solution: vs2017 cannot open the source file stdio h main. H header document [easy to understand]
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)