This article records people in learning cookies to read some of the things to understand, deepen the memory and record down, convenient review.
Encapsulation function
accessing cookies By default is a tricky thing to do. Because cookies store information through strings, it is easy to cause data types that read information to be converted when performing assignment operations. And the string of cookie information is inherently annoying, particularly inconvenient in Web applications that often use cookie information. So you need to encapsulate a cookie function to provide development efficiency!
Defines a function cookie (), which writes the specified cookie information, deletes the specified cookie information, and can read the cookie value for the specified name, and in this function also sets the expiration date, valid path, scope, and security option settings for the cookie information. Complete code:
var Cookie = function (name, value, options) {//If the second argument exists if (typeof value!= ' undefined ') {options = opt ions | |
{};
if (value = = null) {//Set expiration Time Options.expires =-1;
} var expires = '; If there is an event argument entry, and the type is number, or the specific time, then set the event if (options.expires && typeof options.expires = ' number ' | | opt
ions.expires.toUTCString)) {var date;
if (typeof options.expires = = ' number ') {date = new date ();
Date.settime (Date.gettime () + (Options.expires * 24 * 60 * 60 * 1000));
else {date = Options.expires; } expires = ';
Expires= ' + date.toutcstring (); var path = Options.path? '; Path= ' + options.path: ',//set path domain = Options.domain? '; Domain= ' + options.domain: ',//set domain secure = options.secure? '; Secure ': '; Set security measures, true to set directly, otherwise null//Save all string information to array, then call Join () method to string and write Cookie information document. cookie = [name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join (');
else {//If the second argument does not exist var cookievalue = null;
if (document.cookie && document.cookie!= ') {var cookie = Document.cookie.split (';'); for (var i = 0; i < cookies.length i++) {var Cookie = (Cookie[i] | |
""). Replace (/^\s+|\s+$/g, ""); if (cookie.substring (0, name.length + 1) = = (name + ' = ')) {cookievalue = decodeURIComponent (cookie.substring (
Name.length + 1));
Break
}} return cookievalue;
}
};
How to use
Write Cookie information:
Simply write a cookie information
cookie ("User", "Baidu");
Write a cookie message, and set more option
cookies ("User", "Baidu", {
expires:10,//valid for 10 days
path: "/",//////////////////////////
: "Www.baidu.com",///effective domain name
secure:true//Encrypted data transfer
});
2. Read cookie information:
3. Delete Cookie information:
And give you a good package of code.
Write Data
function Writecookie (name, value, days) to a cookie {
//define valid date (valid time of cookie)
var expires = "";
Assign a valid date of
if (days) {
var date = new Date ();
Set the validity period (current time + time period)
Date.settime (Date.gettime () + (days * 24 * 60 * 60 * 1000)),//time period is milliseconds
expires = "; Expires= "+ date.togmtstring ();
}
Assign name to cookie, value and expiration date (validity)
document.cookie = name + "=" + value + Expires + "; path=/";
}
Read cookie data
function Readcookie (name) {
var searchname = name + ' = ';
var cookies = Document.cookie.split (';');
for (Var i=0 i < cookies.length; i++) {
var c = cookies[i];
while (C.charat (0) = = ')
c = c.substring (1, c.length);
if (C.indexof (searchname) = = 0) return
c.substring (searchname.length, c.length)
;
return null;
}
Clear All Cookie
function Erasecookie (name) {
//Set time to 1 clears the data stored in the cookie
Writecookie (name, "",-1);
}
Finally, if there are any errors or questions in the article, please point out. With the SF everybody!