获取URL的name值 getUrl(url,name) 传入url和key 得到key对应的value
<body> <script type="text/javascript"> var url = "http://192.168.1.82:8020/juzhong/getURLvalue.html?name1=zhangyiggg&sex1=nuuu&age1=8090"; var name1 = getUrl(url,"name1"); var sex1 = getUrl(url,"sex1"); var age1 = getUrl(url,"age1"); console.log("name1= "+name1); console.log("sex1= "+sex1); console.log("age1= "+age1); //链接带后缀!用 function getUrl(url,name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = url.split("?")[1].match(reg); // var r = url.split(name+"=")[1].split("!")[0]; if (r != null) return decodeURI(r[2]); return null; } </script> </body>
输出为:
思路:供学习!!
/* https://www.baidu.com/s?name=avery&age=20&sex=male&like=talkwithothers 取出name,age和sex的值 */ var str = "https://www.baidu.com/s?name=avery&age=20&sex=male&like=talkwithothers"; var st1 = str.split("?"); var arr = st1[1]; var st2 = arr.split("&"); for (var i=0;i<=st2.length-1;i++) { var ss = st2[i].split("="); var dd = ss[0]; var ff = ss[1]; if (ff!="") { document.write(" "+dd+":"+ff+"<br />"); } }
我好想爱这个世界啊
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步