js获取地址栏中传递的参数(两种方法)
作者:bicycle-lei 时间:2024-04-17 09:54:30
第一种:字符串拆分法
window.location.href 或者 location.href 或者 window.location 获得地址栏中的所有内容
decodeURI()可以解码地址栏中的数据 恢复中文数据
window.search 获得地址栏中问号及问号之后的数据
//获取地址栏里(URL)传递的参数
function GetRequest(value) {
//url例子:www.bicycle.com?id="123456"&Name="bicycle";
var url = decodeURI(location.search); //?id="123456"&Name="bicycle";
var object = {};
if(url.indexOf("?") != -1)//url中存在问号,也就说有参数。
{
var str = url.substr(1); //得到?后面的字符串
var strs = str.split("&"); //将得到的参数分隔成数组[id="123456",Name="bicycle"];
for(var i = 0; i < strs.length; i ++)
{
object[strs[i].split("=")[0]]=strs[i].split("=")[1]
}
}
return object[value];
}
第二种:正则匹配法
这种方法其实原理和上一种方法类似,都是从URL中提取,只是提取的方法不同而已。
function GetQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
来源:http://www.cnblogs.com/bicycle-lei/p/6377519.html
标签:js,获取,地址栏,参数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
最新MySQL高级SQL语句大全
2024-01-24 22:58:00
![](https://img.aspxhome.com/file/2023/8/72448_0s.png)
Sanic框架蓝图用法实例分析
2022-12-22 16:27:20
SQL Server 2005 Express混合模式登录设置
2009-02-23 13:55:00
Python中三种时间格式转换的方法
2023-10-18 13:11:43
![](https://img.aspxhome.com/file/2023/2/100932_0s.png)
MySQL找出未提交事务的SQL实例浅析
2024-01-23 04:08:25
![](https://img.aspxhome.com/file/2023/0/77310_0s.png)
关于python之字典的嵌套,递归调用方法
2021-12-05 05:25:45
jquery判断单选按钮radio是否选中的方法
2024-04-19 10:18:25
从Python的源码来解析Python下的freeblock
2023-07-26 20:44:39
![](https://img.aspxhome.com/file/2023/7/61217_0s.png)
python实现换位加密算法的示例
2022-08-04 20:46:36
教你用FrontPage2003轻松布局网页
2008-02-24 17:05:00
![](https://img.aspxhome.com/file/UploadPic/20082/24/frontpage2003_79s.gif)
Python下使用Scrapy爬取网页内容的实例
2022-05-29 13:43:24
django项目搭建与Session使用详解
2021-05-19 11:37:31
![](https://img.aspxhome.com/file/2023/2/127712_0s.png)
ASP URL反编码函数代码
2011-02-28 11:04:00
滚动条样式的css代码介绍
2008-10-21 10:56:00
Oracle Session每日统计功能实现
2023-07-22 09:47:24
![](https://img.aspxhome.com/file/2023/7/64087_0s.png)
Python中ttkbootstrap的介绍与基本使用
2023-08-23 15:57:01
![](https://img.aspxhome.com/file/2023/9/91999_0s.gif)
python中安装django模块的方法
2023-08-27 13:30:21
![](https://img.aspxhome.com/file/2023/1/65051_0s.png)
什么是好的设计
2010-02-25 12:22:00
AMP Tensor Cores节省内存PyTorch模型详解
2021-07-08 01:30:32
![](https://img.aspxhome.com/file/2023/4/88964_0s.jpg)
Python使用Cv2模块识别验证码的操作方法
2022-07-03 14:37:19
![](https://img.aspxhome.com/file/2023/8/102848_0s.png)