Adding url parameters to a url and adding parameters to a url and modifying url parameters _ javascript tips

Source: Internet
Author: User
This article introduces how to add url parameters in javascript. Adding parameters to a url involves url-related parameters, if you are interested in adding url parameters in js, refer to the following article to add the url parameter method to the url. If the original url contains the parameter, the following is an example of Code. For details, see the following.

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

Add and modify URL parameters in JavaScript

 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 content is the method for adding url parameters and adding parameters to the url and changing url parameters introduced by the small Editor. I hope it will be helpful to you, for more information, log on to the script home website.

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.