JS operation Cookie

Source: Internet
Author: User
Tags set cookie

 

Var Cookies = {

 

// Read the Cookie. n is the Cookie name.

Get: function (n ){

Var re = new RegExp (n + '= ([^;] *);? ', 'Gi ');

Var r implements re.exe c (document. cookie) | [];

Return (r. length> 1? R [1]: null)

},

// Read the array Cookie. cookieName indicates the Cookie name and keyName indicates the key to be obtained.

GetArray: function (cookieName, keyName ){

Var oRegex1 = newRegExp (cookieName + '= ([^;] +)', 'I ');

Var values1 using oregex1.exe c (document. cookie );

If (values1 & values1.length> 1 ){

Var values2 = values1 [1]

Var oRegex2 = new RegExp (keyName + '= ([^ &] +)', 'I ');

Var oMatch using oregex2.exe c (values2 );

If (oMatch & oMatch. length> 1)

Return (oMatch [1]);

Else

Return '';

}

Else

Return '';

},

// Write Cookies (arrays not writable). n indicates the Cookie name, v indicates the Cookie value, h indicates the time (in hours), p indicates the Cookie Path, and d indicates the Domain, s is secure

Set: function (n, v, h, p, d, s ){

Var t = new Date ();

If (h ){

// 8.64e7 one day 3.6e6 one hour

T. setTime (t. getTime () + (h * 3.6e6 ));

}

Document. cookie = n + '=' + escape (v) + ';' + (! H? '': '; Expires =' + t. toGMTString () + (! P? '': '; Path =' + p) + (! D? '': '; Domain =' + d) + (! S? '': '; Secure') // Set cookie

},

// Delete the Cookie. n indicates the Cookie name, p indicates the Cookie Path, and d indicates the Domain name.

Del: function (n, p, d ){

Var t = newDate (); // obtain the current time

T. setTime (t. getTime ()-10000); // set date to the past time.

Document. cookie = n + '=' + (! P? '': '; Path =' + p) + (! D? '': '; Domain =' + d) + '; expires =' + t. toGMTString ();

}

};

 

From the wjw.verson Column

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.