假如当前地址栏链接为:https://test.com/?type=students&page=3 //获取参数方法1 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---一般用这个 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 二、获取url链接对象取值 //转译 encodeURIComponent("https://zhidao.baidu.com"); //https%3A%2F%2Fzhidao.baidu.com //逆转译 decodeURIComponent("https%3A%2F%2Fzhidao.baidu.com"); //https://zhidao.baidu.com var url = decodeURIComponent(location.search); //包括问号; // var url = 'https://api.zhongshi.cn/h5/login.html?{"type":"question","id":"67359","vip":true,"appid":"wx43992d7d489d8e53"}'; var indexOf = url.indexOf("?"); if (indexOf !=-1) { var a = url.substr(indexOf + 1); var obj = JSON.parse(a); //这个是正确的转换 console.log(obj,'34'); appid = obj.appid; //获取对象中某个参数值 }