写一个JavaScript获取URL参数的通用方法,可以把它放到常用方法的JS文件中调用,直接上代码例子,新手可以学习一下!
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>Document</title> 8 <script> 9 //获取URL地址参数方法 10 function GetRequest() { 11 var url = location.search; 12 var theRequest = new Object(); 13 if (url.indexOf("?") != -1) { 14 var str = url.substr(1); 15 strs = str.split("&"); 16 for (var i = 0; i < strs.length; i++) { 17 theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); 18 } 19 } 20 return theRequest; 21 } 22 23 window.onload = function() { 24 //调用方法 25 var Request = new Object(); 26 Request = GetRequest(); 27 var mapName = decodeURIComponent(Request["MapName"]); //参数名称 28 //输出测试 29 alert(mapName); 30 } 31 </script> 32 </head> 33 <body> 34 35 </body> 36 </html>