JavaScript获取URL参数公共方法

时间:2023-08-22 20:09:31

写一个JavaScript获取URL参数的通用方法,可以把它放到常用方法的JS文件中调用,直接上代码例子,新手可以学习一下!

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
//获取URL地址参数方法
function GetRequest() {
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
} window.onload = function() {
//调用方法
var Request = new Object();
Request = GetRequest();
var mapName = decodeURIComponent(Request["MapName"]); //参数名称
//输出测试
alert(mapName);
}
</script>
</head>
<body> </body>
</html>