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