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.