
Parameters of a line (GET) in Javascript
Practically on each site it is possible to see the links containing in parameters after a sign "?", for example, http: // some.site.com/? id=1. Usually the server script is engaged in processing of such parameters, but sometimes there is a necessity to learn{find out} these parameters inside JavaScript. How it to make, and there will be a story today.
What are, so-called, GET-parameters? Actually is simply a line of the address, but it is accepted, what if in URL-S there is a symbol "?", all symbols after him are parameters. The treatment of parameters - is standard: first there is a name of a variable, then a symbol "=", then value of a variable, variables are divided{shared} by a symbol "¿". To learn{find out} the current address in JavaScript it is possible having read value window.location. Parsit` a line after "?" It is necessary in two passes: first to break into groups "peremennaja=znachenie", and then already to break into components.
Splitting of a line of parameters is facilitated by that, that in JavaScript there is a special line function - split () which result will be a file of lines. That her to use, all over again it is necessary to create object String as the given function is a method of this object. It is done{made} simply:
someVar = new String (" some text ");
Then we divide a line on podstroki:
someArray = someVar.split ("x");
Where "x" - a symbol of division of a line on podstroki. To find in line any symbol, it is necessary to use one more line function - indexOf ():
someVar.indexOf ('? ');
Input in the theory is finished. We shall start practice. I have decided, that all GET-variable should be stored{kept} in two separate global files: one stores{keeps} names, another - values. Unfortunately JavaScript does not support associative files, therefore we shall use the way specified by me. Also it is necessary to save quantity{amount} of GET-variable. Certainly, always it is possible to call function of calculation of the size of a file, but stilisticheski my method is better. And so, global variables and files:
var _GET_Keys; // Meanwhile an empty file
var _GET_Values;
var _GET_Count = 0; // elements while no
var _GET_Default = "
Value of a variable _GET_Default will be explained later. Further I shall create function get_parseGET () which will be parsit` URL and to create files with variables. Right at the beginning function creates object String and checks in him presence of a symbol "?":
get = new String (window.location);
x = get.indexOf ('? ');
if (x! =-1)
{
// Here there will be a basic code
}
else;
Further we cut out the part of the line which is taking place after the found symbol:
l = get.length;
get = get.substr (x+1, l-x);
Now we divide a line into groups "peremennaja=znachenie", we calculate total of variables and we are prepared to sozdainju necessary files:
l = get.split (' and ');
x = 0;
_GET_Count = l.length;
_GET_Keys = new Array (_GET_Count);
_GET_Values = new Array (_GET_Count);
And in summary we divide{share} the received groups on two files with names of variables and their values:
for (i in l)
{
get = l [i] .split (' = ');
_GET_Keys [x] = get [0];
_GET_Values [x] = get [1];
x ++;
}
In the given example the design for.. in, touching all elements of a file is used. Syntax of the given design:
for (key in array)
{
// Your actions
}
Where key - the name of a key which will be chosen from a file array. In a body of a cycle to the current element it is possible bduet to address array [key].
The second function from library - get_fetchVar (key), allows to learn{find out} value of the set GET-variable. Works simple pereborom a file _GET_Keys. If the key is not found, returns value _GET_Default which was mentioned above. I want to notice, that value _GET_Default is not so necessary for changing in the library - if necessary, it can be made in your HTML-code:
<script language = "JavaScript"> _GET_Default = " tra la la "; </script>
Right at the end of a script the call get_parseGET () follows; and on it the library comes to an end.