获取URL里面传的参数,在Js中不能像后台一样使用Request.QueryString来获取URL里面参数,下面介绍两种方式用来获取参数
方式一:使用split分隔来获取,这种方法考试了地址中包含了returnUrl参数的形式,如果地址中本来就包含了另外一个网址,这个时候需要先过滤掉这个网址再去获取参数值,这里只考试另一个网址是最后一个参数的模式
function getParam(paramName,urlParamName) { if(urlParamName==undefined||urlParamName==""){ urlParamName="returnurl"; } paramValue = ""; isFound = false; paramName = paramName.toLowerCase(); var arrSource = this.location.search.substring(1, this.location.search.length).split("&"); if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) { if (paramName == urlParamName) { var retIndex = this.location.search.toLowerCase().indexOf(urlParamName); if (retIndex > -1) { var returnUrl = unescape(this.location.search.substring(retIndex + 10, this.location.search.length)); if ((returnUrl.indexOf("http") != 0) && returnUrl != "" && returnUrl.indexOf(location.host.toLowerCase()) == 0) returnUrl = "http://" + returnUrl; return returnUrl; } } i = 0; while (i < arrSource.length && !isFound) { if (arrSource[i].indexOf("=") > 0) { if (arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase()) { paramValue = arrSource[i].toLowerCase().split(paramName + "=")[1]; paramValue = arrSource[i].substr(paramName.length + 1, paramValue.length); isFound = true; } } i++; } } return paramValue; }
方式二:使用正则表达式获取
function QueryString(name) { var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i")); if (result == null || result.length < 1) { return ""; } return result[1]; }