DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScritp添加url參數並將參數加入到url中及更改url參數的方法
JavaScritp添加url參數並將參數加入到url中及更改url參數的方法
編輯:關於JavaScript     

javascritp添加url參數方法,將參數加入到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 = ; 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參數的方法,希望對大家有所幫助,更多信息請登錄網站了解更多。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved