asp中页⾯间url参数传递的三种实现⽅法要进⾏参数传递的URL为 demo.aspx?name=feiyue&city=beijing
⽅法⼀:javascript⽅法,将url解析放到单独的函数中
function GetRequest() {
var url = decodeURI(location.search); //获取url中"?"符后的字串
var theRequest = new Object();
// if (url.indexOf("?") != -1) {
var str = url.substr(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 objRequest = new Object();
objRequest = GetRequest();
var name = objRequest['name'];
var city = objRequest['city'];
alert("name: " + name + "city: " + city);
⽅法⼆:使⽤JS+正则表达式的⽅法
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造⼀个含有⽬标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配⽬标参数
if (r != null) return unescape(r[2]); return null; //返回参数值
}
var name = objRequest['name'];
var city = objRequest['city'];
alert("name: " + name + "city: " + city);
⽅法三: ⽐较完整的参数获取⽅式js
// 获取地址栏的参数数组
function getUrlParams() {
var searchurl = window.location.search;
/
/ 写⼊数据字典
var tmparray = searchurl.substr(1, searchurl.length).split("&"); var paramsArray = new Array;
if (temparray != null) {
for (var i = 0; i < temparray.length; i++) {
var reg = /[=|^==]/; // ⽤=进⾏拆分,但不包括==
var set1 = temparray[i].replace(reg, '&');
var tempStr2 = set1.split('&');
var array = new Array;
array[tempStr2[0]] = tempStr2[1];
paramsArray.push(array);
}
}
// 将参数数组进⾏返回
return paramsArray;
}
// 根据参数名称获取参数值
function getParamValue(name) {
var paramsArray = getUrlParams();
if (paramsArray != null) {
for (var i = 0 ; i < paramsArray.length ; i++) {
for (var j in paramsArray[i]) {
if (j == name) {
return paramsArray[i][j];
}
}
}
}
return null;
}
var name = getParamValue['name'];
param namevar city = getParamValue['city'];
alert("name: " + name + "city: " + city);
⽅法四:扩展使⽤Jquery⽅式实现
(function($){
$.getUrlParam= function(name)
{
var reg= new RegExp("(^|&)"+name +"=([^&]*)(&|$)");
var r= window.location.search.substr(1).match(reg);
if (r!=null) return unescape(r[2]); return null;
}
})(jQuery);
var name = $.getUrlParam('name');
var city = $.getUrlParam('city');
alert("name: " + name + "city: " + city);
以上四种⽅式都可以实现在页⾯之前进⾏参数的传递,获取URL地址,并解析参数
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论