Home >Web Front-end >JS Tutorial >JavaScript method to add url parameters and add parameters to url and change url parameters_javascript skills

JavaScript method to add url parameters and add parameters to url and change url parameters_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:35:141524browse

javascript adds url parameter method, and adds the parameters to the url. If there is one in the original url, it will be overwritten. The following will explain it to you through a code example. Please see below for the specific content.

js code is as follows:

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 = ; i < arr.length; i++){ 
resObj[arr[]] = arr[]; 
} 
return resObj; 
}; 
var existObjKey = function(existObjKey_obj, str){ 
for(var i in existObjKey_obj){ 
if(i == str){ 
return true; 
} 
} 
return false; 
}; 
var objExtend = function(obj, obj){ 
var result = {}; 
for(var i in obj){ 
if(existObjKey(obj, i)){ 
result[i] = obj[i]; 
}else{ 
result[i] = obj[i]; 
} 
} 
}; 
if(asearch == ''){ 
console.log(obj); 
result = aprotocol + '//' + ahost + apath + '&#63;' + joinObj(obj) + ahash; 
}else{ 
var oldSearchObj = splitSearchToObj(asearch.substr()); 
result = aprotocol + '//' + ahost + apath + joinObj(objExtend(oldSearchObj, obj)) + ahash; 
} 
return result; 
} 
addToUrl({'kd': 'aaa'});

JavaScript added to change URL parameters

 function ChangeParam(name,value)
 {
  var url=window.location.href ;
  var newUrl="";
var reg = new RegExp("(^|)"+ name +"=([^&]*)(|$)");
var tmp = name + "=" + value;
if(url.match(reg) != null)
{
 newUrl= url.replace(eval(reg),tmp);
}
else
{
 if(url.match("[\&#63;]"))
 {
 newUrl= url + "&" + tmp;
 }
 else
 {
 newUrl= url + "&#63;" + tmp;
 }
}
   location.href=newUrl;
 }

The above content is the method introduced by the editor to add url parameters in JavaScript and add the parameters to the url and change the url parameters. I hope it will be helpful to everyone. For more information, please log in to the Script House website to learn more.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn