Cookie attributes
Expires survival
Default Value: exists during the session (the cookie value is destroyed after the user closes the browser)
Others: The date format is normalized to GMT (or UCT) "weekday, DD-mon-yyyy hh: mm: ss gmt". The solution is to use the date object.
Accessible path
Default: the path of the web page that creates the cookie value (the cookie can only be accessed by other pages in the current directory or by pages in the subdirectories of the current directory)
Others: You can set path to "/" so that the entire server domain can access this cookie member.
Domain accessible domain
Default: current domain
Others: Different subdomains cannot access each other.
Secure Security (specifies the way to transmit cookie values over the network)
Default Value: false
Others: false-common and insecure HTTP connections; true-https or other security protocols
Get instances in Date Format
VaR Nextyear = New Date ();
Nextyear. setfullyear (nextyear. getfullyear () + 1 );
Expiretime = Nextyear. togmtstring ();
A simple encapsulation implementation
// Cookie-related operations
Function Cookie (){};
// Set cookie value
// Parameter: Cookie name, value, number of living days, and number of Living Hours
Cookie. setcookie = Function (Name, value, days, hours ){
// If no lifetime is input, the default value is 0 days and 2 hours.
Days = Days | 0 ;
Hours = Hours | 2 ;
VaR Exptime = New Date ();
Exptime. setdate (exptime. getdate () + Days );
Exptime. sethours (exptime. gethours () + Hours );
Document. Cookie = Escape (name) + " = " + Escape (value) +
" ; " + " Expires = " + Exptime. togmtstring ();
};
// Obtain a cookie value
// Returned: cookie value or undefined
Cookie. getcookie = Function (Name ){
VaR RS = Document. Cookie. Match (
New Regexp ( " (? : ^ |) " + Escape (name) + " = ([^;] *) (? :; | $) " , " I " )
);
Return RS ? Unescape (R [ 1 ]): Undefined;
};
//Delete a cookie value
Cookie. deletecookie= Function(Name ){
Document. Cookie=Escape (name)+ "=; Expires =" + NewDate (). togmtstring ();
};
// Clear all cookies
Cookie. clearcookie = Function (){
// Obtain the expiration time string
VaR Expstr = New Date (). togmtstring ();
// Obtains all name values in the currently accessible cookie.
VaR RS = Document. Cookie. Match (
New Regexp ( " ([^;] [^;] *) (? = (= [^;] *) (; | $ )) " , " Gi " )
);
// Delete all cookies
For ( VaR I In RS)
Document. Cookie = RS [I] + " =; Expires = " + Expstr;
};
Cookie. setsubcookie= Function(Name, subname, value ){
};
Cookie. getsubcookie= Function(Name, subname ){
};
Cookie. setfullcookie= Function(Name, value, exptime, path, domain, secure ){
};
Other notes
Cookie is a singleton object.
The cookie value cannot contain semicolons, commas (,), or blank characters. When these special characters appear, you can use the escape function provided by JavaScript to encode them first (Unescape decoding ).
The total number of cookies saved by the browser cannot exceed 300 (note that the number of cookies is a singleton), and the number of cookies saved for each web server cannot exceed 20 (exceeding 20: breadcrumb technology, generally, a colon is used as the sub-Cookie delimiter. The data stored in each cookie cannot exceed 4 kb (solution: userdata, another slightly stronger script-based data storage mechanism)