假如当前地址栏链接为:https://test.com/?type=students&page=3
function UrlSearch()
{
var name,value;
var str=location.href; //取得整个地址栏
var num=str.indexOf("?");
str=str.substr(num+1); //取得所有参数 stringvar.substr(start [, length ]
var arr=str.split("&"); //各个参数放到数组里
for(var i=0;i < arr.length;i++){
num=arr[i].indexOf("=");
if(num>0){
name=arr[i].substring(0,num);
value=arr[i].substr(num+1);
this[name]=value;
}
}
}
var UrlSearch=new UrlSearch(); //实例化
alert(UrlSearch.type); //students
alert(UrlSearch.page); //3
alert(UrlSearch.studentid); //undefined
方法2:
//获取参数方法2
function getParameter(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return decodeURI(r[2])
}
return null
}
alert(getParameter('type')); //students
alert(getParameter('page')); //3
alert(getParameter('studentid')); //null