js querystring
第一种代码:(最喜欢这种简洁的代码)
<script>
//取得传值的函数
function QueryString(qs)
{
s = location.href;
s = s.replace("?","?&").split("&");
re = "";
for(i=1;i<s.length;i++)
{
if(s[i].indexOf(qs+"=")==0)
{
re = s[i].replace(qs+"=","");
}
}
return re;
}
</script>
第二种代码:
// author: never-online
// web: never-online.net
// 获取request
var request = {
QueryString : function(val) {
var uri = window.location.search;
var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig");
return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
},
QueryStrings : function() {
var uri = window.location.search;
var re = /\w*\=([^\&\?]*)/ig;
var retval=[];
while ((arr = re.exec(uri)) != null)
retval.push(arr[0]);
return retval;
},
setQuery : function(val1, val2) {
var a = this.QueryStrings();
var retval = "";
var seted = false;
var re = new RegExp("^" +val1+ "\=([^\&\?]*)$", "ig");
for(var i=0; i<a.length; i++) {
if (re.test(a[i])) {
seted = true;
a[i] = val1 +"="+ val2;
}
}
retval = a.join("&");
return "?" +retval+ (seted ? "" : (retval ? "&" : "") +val1+ "=" +val2);
}
}
function getRe(){
alert(request.QueryString(''));
alert('asdf');
}
先放上 有空了仔细研究一下:)
<script>
//取得传值的函数
function QueryString(qs)
{
s = location.href;
s = s.replace("?","?&").split("&");
re = "";
for(i=1;i<s.length;i++)
{
if(s[i].indexOf(qs+"=")==0)
{
re = s[i].replace(qs+"=","");
}
}
return re;
}
</script>
第二种代码:
// author: never-online
// web: never-online.net
// 获取request
var request = {
QueryString : function(val) {
var uri = window.location.search;
var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig");
return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
},
QueryStrings : function() {
var uri = window.location.search;
var re = /\w*\=([^\&\?]*)/ig;
var retval=[];
while ((arr = re.exec(uri)) != null)
retval.push(arr[0]);
return retval;
},
setQuery : function(val1, val2) {
var a = this.QueryStrings();
var retval = "";
var seted = false;
var re = new RegExp("^" +val1+ "\=([^\&\?]*)$", "ig");
for(var i=0; i<a.length; i++) {
if (re.test(a[i])) {
seted = true;
a[i] = val1 +"="+ val2;
}
}
retval = a.join("&");
return "?" +retval+ (seted ? "" : (retval ? "&" : "") +val1+ "=" +val2);
}
}
function getRe(){
alert(request.QueryString(''));
alert('asdf');
}
先放上 有空了仔细研究一下:)