Add the url parameter to the url, and then add the parameter to the url.

Source: Internet
Author: User

Add the url parameter to the url, and then add the parameter to the url.

Add object parameters to the url. If the original url contains parameters
Js Code

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 = 0; i < arr.length; i++){ resObj[arr[0]] = arr[1]; } return resObj; }; var existObjKey = function(existObjKey_obj, str){ for(var i in existObjKey_obj){ if(i == str){ return true; } } return false; }; var objExtend = function(obj1, obj2){ var result = {}; for(var i in obj1){ if(existObjKey(obj2, i)){ result[i] = obj2[i]; }else{ result[i] = obj1[i]; } } }; if(asearch == ''){ console.log(obj); result = aprotocol + '//' + ahost + apath + '?' + joinObj(obj) + ahash; }else{ var oldSearchObj = splitSearchToObj(asearch.substr(1)); result = aprotocol + '//' + ahost + apath + joinObj(objExtend(oldSearchObj, obj)) + ahash; } return result; } addToUrl({'kd': 'aaa'}); 

How to obtain url parameters using javascript

Javascript can be obtained, but only on the current page.
<Script type = "text/javascript">
Var url = document. location,
Args;

Args = String (url). split ('? ');
If (args [1]) args = args [1];

Alert (args );
</Script>
Note that String (url) conversion is essential because the url is previously an obejct.

Error code reading URL parameters in javascript

This code can be written in this way.
<Script language = "javascript">

Window. onload = function () {var urlParts = document. URL. split ("? ");

Var parameterParts = urlParts [1]. split ("&");

For (I = 0; I <parameterParts. length; I ++ ){
Var pairParts = parameterParts [I]. split ("= ");
Var pairName = pairParts [0];
Var pairValue = pairParts [1];
Document. write (pairName + ":" + pairValue );
}

}
</Script>

The code itself is the parameter value and parameter name passed in the real url, so the url needs to be relatively complex
If there is no "?" in the url, That is to say, if no parameter is input, it will not be displayed.
The code I added above is automatically executed after loading the webpage!
Hope you have a good time.

Related Article

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.