We often see that some Page Link addresses are followed by parameters. In many cases, we need to obtain the values of these parameters. Next we will introduce how to obtain them. Interested friends can understand the system, I hope this article will help you find a useful function urlArgs IN THE RHINO book today (extracting parameters in the URL search string ). We often see some page link address followed by parameters, such as http://www.xxx.com /? Username = yyy & password = zzz and so on. Many times we need to obtain the values of these parameters (yyy and zzz), then we can use the urlArgs function, this function is obtained through the attribute of the return value (the returned value is an object) of the function.
UrlArgs Function Code:
The Code is as follows:
Function urlArgs (){
Var args = {};
Var query = location. search. substring (1 );
Var pairs = query. split ('&');
For (var I = 0; I <pairs. length; I ++ ){
Var pos = pairs [I]. indexOf ('= ');
If (pos =-1) continue;
Var name = pairs [I]. substring (0, pos );
Var value = pairs [I]. substring (pos + 1 );
Value = decodeURIComponent (value );
Args [name] = value;
}
Return args;
}
Usage:
The Code is as follows:
Var args = urlArgs ();
Var username = args. username; // yyy
Var password = args. password; // zzz