文章目录
  1. 1. 通过正则
  2. 2. 通过切串放进数组

JS获取url:
window.location.href;

有个url格式如下:
http://localhost/lab/GM-center/src/user_manage/user_info?game=tank&appid=1009

通过正则

1
2
3
4
5
6
7
8
9
function getQueryString(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}

var from = getQueryString("game");

alert(from);

通过切串放进数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function GetRequest() { 
var url = location.search; //获取url中"?"符后的字串
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;
}

var req = GetRequest();

var from = req['game'];

alert(from);
文章目录
  1. 1. 通过正则
  2. 2. 通过切串放进数组