如何获取url地址中“?”后面的参数
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>js获取url后面的参数</title> 5 <meta charset="utf-8" /> 6 <script> 7 var params=(function(){ 8 var search=location.search; //获取location的search属性,保存在search中 9 var params={}; //创建空对象params 10 if(search!=""){ //如果search不是空字符串 11 search.slice(1).split("&").forEach( //?username=zhangdong&pwd=123456;//search去开头?,按&切割为数组,forEach 12 function(val){ 13 var arr=val.split("="); //将当前元素值按=切割,保存在arr中 14 params[arr[0]]=arr[1]; //向params中添加一个元素,属性名为arr[0],值为arr[1] 15 16 } 17 ); 18 } 19 return params; //返回param 20 })(); 21 console.dir(params); //params:{username:"katherine",pwd:"123456"} 22 </script> 23 </head> 24 <body> 25 <form> 26 姓名:<input name="username"/> 27 密码:<input type="password" name="pwd"/> 28 </form> 29 </body> 30 </html>