通过 javascript 获取当前url地址相关信息


//获取当前url地址
var curUrlPath=window.document.location.href; 
假如当前url地址为:http://localhost:8080/agenda/static/groupSchedule.html?teamid=3&teamname=team2
//获取主机地址之后的部分:/agenda/static/groupSchedule.html
var pathName=window.document.location.pathname; 

//获取主机地址:http://localhost:8080
var pos = curUrlPath.indexOf(pathName);
var localhostPath = curUrlPath.subString(0,pos);
//"?"后的部分,又称为查询字符串:?teamid=3&teamname=team2
var searchURL = window.document.location.search; 
//获取"?"后传的参数部分:teamid=3&teamname=team2
var parameter = searchURL.substring(1, url.length);
//获取参数的值
var teamid = parameter.split("&")[0].split("=")[1];//3
var teamname =decodeURIComponent(searchURL.split("&")[1].split("=")[1],"UTF-8") ;//team2

这里的decodeURIComponent主要是为了对中文进行解码
关于decodeURIComponent() 方法对encodeURIComponent()方法编码的字符串进行解码。可以学习这篇文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值