javascritp添加url参数将参数加入到url中
将对象参数加入到url中,如果原来url中有则覆盖
js代码
functionaddToUrl(obj){
varaprotocol=location.protocol;
varahost=location.host;
varapath=location.pathname;
varasearch=location.search;
varahash=location.hash;
varresult='';
console.log(obj);
varjoinObj=function(joinObj_obj){
varresult='';
for(variinjoinObj_obj){
result+=i+'='+joinObj_obj[i];
}
returnresult;
};
varsplitSearchToObj=function(str){
varresObj={};
vararr=str.split('&');
for(vari=0;i<arr.length;i++){
resObj[arr[0]]=arr[1];
}
returnresObj;
};
varexistObjKey=function(existObjKey_obj,str){
for(variinexistObjKey_obj){
if(i==str){
returntrue;
}
}
returnfalse;
};
varobjExtend=function(obj1,obj2){
varresult={};
for(variinobj1){
if(existObjKey(obj2,i)){
result[i]=obj2[i];
}else{
result[i]=obj1[i];
}
}
};
if(asearch==''){
console.log(obj);
result=aprotocol+'//'+ahost+apath+'?'+joinObj(obj)+ahash;
}else{
varoldSearchObj=splitSearchToObj(asearch.substr(1));
result=aprotocol+'//'+ahost+apath+joinObj(objExtend(oldSearchObj,obj))+ahash;
}
returnresult;
}
addToUrl({'kd':'aaa'});