JAVASCRITP adding URL parameters and adding parameters to the URL and changing the URL parameters _javascript tips

Source: Internet
Author: User

JAVASCRITP Add the URL parameter method, the parameter added to the URL, if the original URL is covered, the following through the code examples for you to explain the details, please see below.

The JS code looks like this:

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-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 add change URL parameter

 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;
 }

The above is a small set to introduce JAVASCRITP add URL parameters and adding parameters to the URL and change the URL parameters of the method, I hope to help you, more information please visit the cloud-Habitat community website to learn more.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.