window.location对象 获取页面地址

window.location对象的属性:

属性含义
location.protocol协议"http://"或"https://"
location.hostname服务器名字"baidu.com"
location.port端口"8080"
location.pathnameURL中主机名后的部分"/index.php"
location.search"?"后的部分,又称查询字符串"?type=2&id=122"
location.hash"#"之后的内容"#first"
location.host等于hostname+port"baidu.com:8080"
location.href当前页面的完整URL"http://baidu.com:8080?type=2&id=50#first"

 

window.location对象的方法:

方法描述
location.assign()加载新的文档
location.reload()重新加载当前文档
location.replace()用新的文档替换当前文档

 

location.href 与location.assign()以及location.replace()的区别:

location.href='http://baidu.com'    ===  location.assign('http://baidu.com')   都是在当前页面跳转到新的页面,在新页面点击返回按钮,可回到上一页。

location.replace('http://baidu.com') 当前页面被新页面替换,不能回到上一页。

 

如何使用js脚本捕获页面GET方法请求的参数?    比如 "?type=2&id=50" 里的2和50。

1 var dataList=window.location.search;  //使用location.search获取?type=2&id=50字符串。
2 var dataArray=dataList.split("&");    //用"&"将字符串进行分割,返回到数组中。得到{?type=2,id=50}
3 var type=dataArray[0].split("=")[1];  //用"="将数组中的"?type=2"分割为数组{?type,2},并取index=1的字符串2。
4 var id=dataArray[1].split("=")[1];    //同上
5 console.log(type,id); //2  50

 

补充:字符串的split()方法

split(separator,howmany);   

separator:必需,字符串或正则表达式。从该参数指定地方分割。被分割的字符返回到数组中,且不包含separator本身。

howmany:可选,指定返回数组的最大长度。

 

转载于:https://www.cnblogs.com/zhuqs/p/9238711.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值