If you have a better opinion, you can discuss it!
Copy Code code as follows:
/*
* Js Class Cookie
* AUTHOR:MR Co
*/
var Cookie = function (/*cookie name */name) {
this. $name = name;
var allcookies = Document.cookie;
if (allcookies = = ") return;
var cookies = Allcookies.split (';');
var cookie = null;
for (var i = 0; i < cookies.length; i++) {
if (cookies[i].substring (0,name.length + 1) = = (name + ' = ')) {
cookie = cookies[i];
Break
}
}
if (cookie = null) return;
var cookieval = cookie.substring (name.length + 1);
var a = Cookieval.split (' & ');
for (var i = 0; i < a.length; i++) {
A[i] = a[i].split (': ');
}
for (var i = 0; i < a.length i + +) {
This[a[i][0]] = decodeURIComponent (a[i][1]);
}
}
/*
* Save Cookie Data Object
*/
Cookie.prototype.store = function (/* Expiration time (1 means one day, etc.) */daystolive,/* current cookie valid address */path,/* current cookie valid domain name Access */domain,/* Security */secure) {
var cookieval = ';
For (Var prop in this) {
if ((Prop.charat (0) = = ' $ ') | | ((typeof this[prop]) = = ' function ') Continue
if (Cookieval!= ') Cookieval + = ' & ';
Cookieval + = prop + ': ' + encodeuricomponent (This[prop]);
}
var cookie = this. $name + ' = ' + cookieval;
if (daystolive | | daystolive = = 0) {
cookies = '; Max-age= ' + (daystolive * 24 *60 *60);
}
if (path) cookie = '; Path= ' + path;
if (domain) cookie = ';d omain= ' + domain;
if (secure) cookie = '; secure ';
Document.cookie = cookie;
}
/*
* Remove Cookie Data Object specified property
*/
Cookie.prototype.remove = function (/* Current cookie valid address */path,/* current cookie valid domain name Access */domain,/* security */secure) {
For (Var prop in this) {
if (Prop.charat (0)!= ' $ ' && typeof This[prop]!= ' function ') delete This[prop];
}
This.store (0,path,domain,secure);
}
/*
* Verify that the current client browser supports cookies
*/
Cookie.isallowcookie = function () {
if (!navigator.cookieenabled) {
Alert (' warm tip: \ r \ n Your browser is currently disabled page cookie! This may cause you to refresh the page and lose your selected food data when \r\n\r\n Selects the food data! \r\n\r\n Recommends that you enable browser cookie! ');
return false;
}
return true;
}
Test JS DEMO
Copy Code code as follows:
function Testfn () {
var cookie = new Cookie (' Test ');
if (!cookie.name | |!cookie.color) {
Cookie.name = Prompt (' What is your name: ', ');
Cookie.color = Prompt (' What is your favorite color: ', ');
}
if (!cookie.visits) cookie.visits = 1;
else cookie.visits++;
Cookie.store (10);
Alert (' Color: ' + cookie.color + ' name: ' + cookie.name + ' visits: ' + cookie.visits);
}