Javascript extracts parameters from the URL search string (custom function implementation)

Source: Internet
Author: User

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

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.