javascritp url 매개 변수 추가 url에 매개 변수 추가

1569 단어
대상 파라미터를 URL에 추가하고 원래 URL에 있으면 js 코드를 덮어씁니다

function addToUrl(obj){ 
var aprotocol = location.protocol; 
var ahost = location.host; 
var apath = location.pathname; 
var asearch = location.search; 
var ahash = location.hash; 
var result = ''; 
console.log(obj); 
var joinObj = function(joinObj_obj){ 
var result = ''; 
for(var i in joinObj_obj){ 
result += i + '=' + joinObj_obj[i]; 
} 
return result; 
}; 
var splitSearchToObj = function(str){ 
var resObj = {}; 
var arr = str.split('&'); 
for(var i = 0; i < arr.length; i++){ 
resObj[arr[0]] = arr[1]; 
} 
return resObj; 
}; 
var existObjKey = function(existObjKey_obj, str){ 
for(var i in existObjKey_obj){ 
if(i == str){ 
return true; 
} 
} 
return false; 
}; 
var objExtend = function(obj1, obj2){ 
var result = {}; 
for(var i in obj1){ 
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{ 
var oldSearchObj = splitSearchToObj(asearch.substr(1)); 
result = aprotocol + '//' + ahost + apath + joinObj(objExtend(oldSearchObj, obj)) + ahash; 

} 
return result; 
} 
addToUrl({'kd': 'aaa'}); 

좋은 웹페이지 즐겨찾기