function Uri()
{
this.AppendSearchKey=function(queryString,key,newValue) //替换的可能是查询串
{
var dictionary=this.SplitQueryString(queryString);
var found=false;
for(var i=0;i<dictionary.keys.length;i++)
{
if(dictionary.keys[i]==key)
{
dictionary.values[i]=newValue;
this.found=true;
break;
}
}
if(!found)
{
dictionary.keys.push(key);
dictionary.values.push(newValue);
}
var _queryString=this.JoinQueryString(dictionary.keys,dictionary.values);
return _queryString;
}
this.JoinQueryString=function(keys,values) //组合URL参数,带?
{
var queryString="";
for(var i=0;i<keys.length;i++)
{
if(values[i] && values[i]!="")
{
if(queryString!="") queryString+="&";
queryString+=keys[i]+"="+values[i];
}
}
if(queryString!="") queryString="?"+queryString;
return queryString;
}
this.JoinUrl=function(url,queryString) //自动合并URL和查询字符串
{
if(queryString=="") return url;
var index=url.indexOf("?",0);
var index2=queryString.indexOf("?",0);
if(index==-1)
{
//参数中是否含有问号,问号后面是否有数据
if(index2==-1) url+="?"+queryString;
else url+=queryString;
}
else
{
//URL中含有问号,问号后面是否有数据
if(index==url.length)
{
//url后面无数据
if(index2==-1) url+=queryString;
else url+=queryString.substr(1,queryString.length);
}
else
{
//url后面有数据
if(index2!=-1) queryString=queryString.substr(1,queryString.length);
url+="&"+queryString;
}
}
return url;
}
this.ReplaceUrlKey=function(url,key,newValue) //替换URL中的KEY的值
{
var index=url.indexOf("?",0);
if(index==-1) return;
var queryString=url.substr(index);
if(!queryString || queryString=="") return null;
var href=url.substr(0,index);
var dictionary=this.SplitQueryString(queryString);
for(var i=0;i<dictionary.keys.length;i++)
{
if(dictionary.keys[i]==key)
{
dictionary.values[i]=newValue;
break;
}
}
var _queryString=this.JoinQueryString(dictionary.keys,dictionary.values);
return href+_queryString;
}
this.ReplaceSearchKey=function(queryString,key,newValue) //替换查字符串中的KEY的值
{
var dictionary=this.SplitQueryString(queryString);
for(var i=0;i<dictionary.keys.length;i++)
{
if(dictionary.keys[i]==key)
{
dictionary.values[i]=newValue;
break;
}
}
var _queryString=this.JoinQueryString(dictionary.keys,dictionary.values);
return _queryString;
}
this.SplitQueryString=function(queryString) //拆分URL参数
{
if(queryString.indexOf("?",0)==0) queryString=queryString.substr(1,queryString.length-1);
var dictionary={"keys":[],"values":[]};
var arr=queryString.split("&");
for(var i=0;i<arr.length;i++)
{
var temp=arr[i].split("=");
if(temp.length==2)
{
dictionary.keys.push(temp[0]);
dictionary.values.push(temp[1]);
}
}
return dictionary;
}
}
var Uri=new Uri(); //实例化同名的,直接调用
URL字符串相关操作处理
最新推荐文章于 2022-04-03 17:48:37 发布