JS Object-oriented programming for COOKIE_JS object-oriented

Source: Internet
Author: User
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);
}

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.