JS gets the parameters in the URL link

Source: Internet
Author: User

JS gets the parameters in the URL link:
URL Pass parameters This is common sense, here is not much introduced, since the parameters are passed to get the value of the parameter, the following describes how to implement this function.
The code is as follows:

functionrequest (paras) {varUrl= "softwhy.com?a=1&b=2"; varParastring=url.substring (Url.indexof ("?") +1,url.length). Split ("&"); varparaobj={}     for(vari=0;i<parastring.length;i++){    varj=Parastring[i]; Paraobj[j.substring (0,j.indexof ("=")). toLowerCase ()]=j.substring (j.indexof ("=") +1, j.length); }    varReturnvalue=paraobj[paras.tolowercase ()]; if(typeof(returnvalue) = = "Undefined"){      return""; }  Else{      returnreturnvalue; }} console.log (Request (A)); Console.log (Request (' B '));

The above code implements the function that we want, can output the value of parameters in the specified URL, the following is a brief introduction to the implementation process.
I. Principle of implementation:
The principle is actually very simple, a sentence is to intercept the string in the correct position, first intercept the string, and then use & split the string to generate an array, and then by intercepting the string, the parameter name and parameter value as the key value pairs into the object direct amount, the principle is roughly the same.
Two. Code comments:
1.function request (param) {}, gets the parameter worth the function, parameter is the parameter name passed in the URL.
2.var url= "softwhy.com?a=1&b=2", which will get the URL link address of the parameter.
3.var parastring=url.substring (Url.indexof ("?") +1,url.length). Split ("&"), Url.substring (Url.indexof ("?") +1,url.length) Use this intercept first? Later characters, and then use the split () function to split the string to generate a function.
4.var paraobj={}, declares the direct amount of an object.
5.for (Var i=0;i<parastring.length;i++) {}, iterating through each element in the array.
6.var j=parastring,Assigns the elements in the array to J.
7.paraobj[j.substring (0,j.indexof ("=")). toLowerCase ()]=j.substring (j.indexof ("=") +1,j.length), j.substring (0, J.indexof ("=")). toLowerCase () Gets the name of the URL parameter, j.substring (j.indexof ("=") +1,j.length) gets the parameter value, which is the parameter name as the property name, and the argument value as the property value.
8.var returnvalue=paraobj[param.tolowercase ()], specifies that the property value of the property name is assigned to the variable returnvalue.
9.if (typeof (returnvalue) = = "undefined") {return "}, if the returnvalue value is undefined, that is, it does not exist, returns a null character.
10. Otherwise return returnvalue.
three. Related reading:
The 1.substring () function can be found in thethe substring () method of a JavaScript string objectA chapter.
The 2.indexOf () function can be found in thethe IndexOf () method of a JavaScript string objectA chapter.
The 3.split () function can be found in theThe split () method of a JavaScript string objectA chapter.
The 4.toLowerCase () function can be found in thethe toLowerCase () method of a JavaScript string objectA chapter.

The original address is: http://www.softwhy.com/forum.php?mod=viewthread&tid=9555

For more information, refer to: http://www.softwhy.com/javascript/

JS gets the parameters in the URL link

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.