JavaScritp添加url参数并将参数加入到url中及更改

网络编程 2021-07-04 21:03www.168986.cn编程入门
这篇文章给大家介绍javascript添加url参数方法,将参数加入到url中,涉及到url添加参数的相关知识,关于js添加url参数感兴趣的朋友可以参考下本篇文章

javascritp添加url参数方法,将参数加入到url中,如果原来url中有则覆盖,狼蚁网站SEO优化通过代码实例给大家讲解,具体内容请看下文。

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 = ; 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 + '?' + joinObj(obj) + ahash; 
}else{ 
var oldSearchObj = splitSearchToObj(asearch.substr()); 
result = aprotocol + '//' + ahost + apath + joinObj(objExtend(oldSearchObj, obj)) + ahash; 
} 
return result; 
} 
addToUrl({'kd': 'aaa'});

JavaScript添加更改URL参数 

 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("[\?]"))
 {
 newUrl= url + "&" + tmp;
 }
 else
 {
 newUrl= url + "?" + tmp;
 }
}
   location.href=newUrl;
 }

以上内容是长沙网络推广给大家介绍的JavaScritp添加url参数并将参数加入到url中及更改url参数的方法,希望对大家有所帮助,更多信息请登录狼蚁SEO网站了解更多。

Copyright © 2016-2025 www.168986.cn 狼蚁网络 版权所有 Power by