Parse the JavaScript class of QueryString

Source: Internet
Author: User

A previously written JS class that parses QueryString. You can get or set the value in QueryString, and noted down the value for convenience :)

Example:Copy to ClipboardReference: [www.bkjia.com] var qs = new QueryString (query );

Qs. set ("ajaxids", ids)
Qs. set ("ajaxsn", new Date ())

Query = qs. toStr ();

It can also be called consecutively:

Copy to ClipboardReference: [www.bkjia.com] query = new QueryString (query). set ("ajaxids", ids). set ("ajaxsn", new Date (). toStr ();

For other usage, let's take a look at the code.

Complete code:

Copy to ClipboardReference: [www.bkjia.com] QueryString = function (qs ){
This. p = {};

If (! Qs)
Url = location. search;

If (qs ){
Var B = qs. indexOf ('? ');
Var e = qs. indexOf ('#');

If (B> = 0 ){
Qs = e <0? Qs. substr (B + 1): qs. substring (B + 1, e );

If (qs. length> 0 ){
Qs = qs. replace (/\ +/g ,'');

Var a = qs. split ('&');

For (var I = 0; I <a. length; I ++ ){
Var t = a [I]. split ('= ');
Var n = decodeURIComponent (t [0]);
Var v = (t. length = 2 )? DecodeURIComponent (t [1]): n;
This. p [n] = v;
}
}
}
}

This. set = function (name, value ){
This. p [name] = value;
Return this;
};

This. get = function (name, def ){
Var v = this. p [name];
Return (v! = Null )? V: def;
};

This. has = function (name ){
Return this. p [name]! = Null;
};

This. toStr = function (){
Var r = '? ';
For (var k in this. p ){
R + = encodeURIComponent (k) + '=' + encodeURIComponent (this. p [k]) + '&';
}
Return r;
};
};

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.