JavaScript extracts parameters in the search string of URLs (custom function implementation) _javascript tips

Source: Internet
Author: User
A useful function was found today in the Rhino book Urlargs (extracts the parameters in the search string for the URL). 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 get the values of these parameters (YYY and ZZZ), You can then take advantage of the Urlargs function, which is obtained by the property of the return value of the function (the return value is an object).

Urlargs Function Code
Copy Code code 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;
}

How to use
Copy Code code 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.