js地址栏参数的获取
Introduce
之前在博客中写过,window.location.href的各个字段的值和属性,见博客https://blog.csdn.net/YJD19970908/article/details/81189583,这这种有个方法 window.location.search.比如
//url是 https://www.baidu.com/?a=1&b=2
console.log(window.location.search) //?a=1&b=2
Question
那么如何简单快捷的拿到a的值或者b的值呢?
Idea
- 首先window.loaction.search 取得?a=1&b=2
- 删除掉 ?
- 以&为间隔,把字符串拆分为数组
- 以=为间隔,把一维数组转为二维数组
- 遍历一维数组,看数组的第一个元素和我要查询的是否相同,相同则输出
Code