Today, we found a useful function urlArgs in the rhino book (extract 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
Var args = urlArgs ();
Var username = args. username; // yyy
Var password = args. password; // zzz