JS Cookie:js Operation Cookie

Source: Internet
Author: User
Tags date array current time return set cookie domain

var Cookies = {

Read cookie, n is cookie name
Get:function (n) {
var re = new RegExp (n + ' = ([^;] *);? ', ' GI ';
var r =re.exec (Document.cookie) [];
Return (r.length> 1? R[1]: null)
},
Read array cookie,cookiename as cookie name, KeyName for key to get
Getarray:function (CookieName, KeyName) {
var oRegex1 = newregexp (cookiename + ' = [^;] +) ', ' I ';
var values1 =oregex1.exec (Document.cookie);
if (values1&& values1.length >1) {
var values2 =values1[1]
var oRegex2 =new RegExp (keyname + ' = ([^&]+) ', ' I ');
var omatch =oregex2.exec (Values2);
if (omatch&& omatch.length >1)
Return (omatch[1]);
Else
Return ";
}
Else
Return ";
},
Write cookies (not writable array), n is cookie name, V is cookie value, H is time (in hours), p is cookie path path, D is domain, S is secure
Set:function (n,v,h,p,d,s) {
var t = new Date ();
if (h) {
8.64e7 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 cookie, n is cookie name, p is cookie path path, D is domain
Del:function (n,p,d) {
var t = newdate ();//Get current time
T.settime (T.gettime ()-10000);//Set Date to past time
Document.cookie = n + ' = ' + (!p? '' : '; Path= ' + P ' + (!d?) '' : '; Domain= ' + D) + '; Expires= ' + t.togmtstring (); This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121102/33572.html

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.