Using JavaScript to get parameters submitted through the Get method in a Web page

Source: Internet
Author: User

Below I will write a function to get the parameters of the Get method submission

functionGetParameter (parametername) {varString =Window.location.search; varIndexstart = String.IndexOf (parametername+ "="); if(Indexstart==-1)        return false; varresult = String.slice (indexstart+parametername.length+1); varIndexend = Result.indexof ("&"); if(Indexend!=-1) Result= Result.slice (0, Indexend); returndecodeURI (result);}
Full Code

I'll walk through each step step by step.

Suppose our URL is www.****.com?a=1&bc=23&d=5, and we want to take the value of BC

var string = Window.location.search;

This step, the URL in the "? "The following string, that is," a=1&bc=23&d=5 ", is saved in the variable string.

var indexstart = String.IndexOf (parametername+ "="); if (Indexstart==-1)     return false;

This step will look for the first occurrence of "bc=" in the string, and if "bc=" is not found, it will end the function and return false.

var result = String.slice (indexstart+parametername.length+1);

This time, we'll save all the characters after "bc=" in the result variable. This is, of course, the beginning of our final result.

var indexend = Result.indexof ("&");    if (indexend!=-1)        result = Result.slice (0,indexend);

This time we are looking for the end of the result, usually the "&" symbol as the end of the place. Of course, if it is in string as the last argument, such as "d=5" this, then our previous result is already the correct result, do not need this step.

return decodeURI (Result);

Finally we return to result as the final outcome. Because if the argument in Chinese words, will be garbled, so we add a decodeURI function here to return the correct Chinese characters.

Well, we're done here!

Use JavaScript to get parameters submitted through the Get method in a Web page

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.