//获取指定参数,返回字符串;
2、request.QueryStrings();
//获取全部参数,并返回数组;
3、request.setQuery("参数","参数的值");
//如果当前地址栏有此参数,那么将更新此参数,否则返回一个新的地址栏参数字符串。
例如:
当前地址栏参数字符串为:?name=a&site=never_online
alert(request.setQuery("name","blueDestiny"))
如果地址栏参数中有"name",那么返回?name=blueDestiny&site=never_online
setQuery方法有自动追加参数的功能。如:
当前地址栏参数字符串为:?site=never_online
alert(request.setQuery("name","blueDestiny"))
则返回?site=never_online&name=blueDestiny
同理,如果地址栏没有参数,也会自动追加参数
alert(request.setQuery("name","blueDestiny"))
返回?name=blueDestiny
1<SCRIPT LANGUAGE="JavaScript">
2<!--
3// author: never-online
4// web: never-online.net
5var request = {
6 QueryString : function(val) {
7 var uri = window.location.search;
8 var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig");
9 return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
10 },
11 QueryStrings : function() {
12 var uri = window.location.search;
13 var re = /\w*\=([^\&\?]*)/ig;
14 var retval=[];
15 while ((arr = re.exec(uri)) != null)
16 retval.push(arr[0]);
17 return retval;
18 },
19 setQuery : function(val1, val2) {
20 var a = this.QueryStrings();
21 var retval = "";
22 var seted = false;
23 var re = new RegExp("^" +val1+ "\=([^\&\?]*)$", "ig");
24 for(var i=0; i<a.length; i++) {
25 if (re.test(a[i])) {
26 seted = true;
27 a[i] = val1 +"="+ val2;
28 }
29 }
30 retval = a.join("&");
31 return "?" +retval+ (seted ? "" : (retval ? "&" : "") +val1+ "=" +val2);
32 }
33}
34alert(request.setQuery("e","b"))
35//-->
36</SCRIPT>
2<!--
3// author: never-online
4// web: never-online.net
5var request = {
6 QueryString : function(val) {
7 var uri = window.location.search;
8 var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig");
9 return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
10 },
11 QueryStrings : function() {
12 var uri = window.location.search;
13 var re = /\w*\=([^\&\?]*)/ig;
14 var retval=[];
15 while ((arr = re.exec(uri)) != null)
16 retval.push(arr[0]);
17 return retval;
18 },
19 setQuery : function(val1, val2) {
20 var a = this.QueryStrings();
21 var retval = "";
22 var seted = false;
23 var re = new RegExp("^" +val1+ "\=([^\&\?]*)$", "ig");
24 for(var i=0; i<a.length; i++) {
25 if (re.test(a[i])) {
26 seted = true;
27 a[i] = val1 +"="+ val2;
28 }
29 }
30 retval = a.join("&");
31 return "?" +retval+ (seted ? "" : (retval ? "&" : "") +val1+ "=" +val2);
32 }
33}
34alert(request.setQuery("e","b"))
35//-->
36</SCRIPT>