Cookie attributes, expiration and scope

Source: Internet
Author: User

/*
* Cookiestorage API
* @maxage Validity
* Path scope
* */

function Cookiestorage (maxage, path) {var cookie = (function () {var cookie = {};
var all = Document.cookie; Get information on all cookies
Returns an empty object if it is an empty string
if (all = = = "")
return cookie;
Separating famous/value pairs
var list =all.split (";");
Iterate through each cookie
for (var i = 0; i < list.length; i++) {
var cookie = list[i];
Console.log (cookie);
var p = cookie.indexof ("="); Find the first "=" symbol
var name = cookie.substring (0, p); Get the name of the cookie
var value = cookie.substring (p + 1); Get the value of the cookie
Value = decodeuricomponent (value); To store a name value pair in an object
if (name = = = "UserId") {
Continue
}
Cookie[name] = value;
}
return cookie;
}());
Store the names of all cookies in an array
var keys = [];
for (var key in cookie) Keys.push (key);

Defining properties and methods for storage API public
The number of cookies stored
This.length = Keys.length;
Console.log (this.length);

Returns the name of the nth cookie and returns NULL if n is out of bounds
This.key = function (n) {
if (N < 0 | | n > Keys.length) {
return null
}
return keys[n];
};

Returns the cookie value for the specified name, or null if it does not exist

This.getitem = function (name) {
return Cookie[name] | | Null
};

Store cookie Values
This.setitem = function (key, value) {
if (! (Key in Cookie)) {
Keys.push (key);
this.length++;
}
Store the name/value pair data in a cookie object
Cookie[key] = value;
Start the formal setting of cookies
First encode the value of the cookie to be stored colleague creates a string in the form of a "First name = encoded value"
var cookie = key + "=" +encodeuricomponent (value);
Add the attributes of a cookie to the string
if (maxage) cookie + = "; Max-age= "+ maxage;
if (path) cookie + = "; Path= "+ path;

Setting cookies with the Document.cookie property
Document.cookie = cookie;
};

Delete the specified cookie
This.removeitem = function (key) {
if (! (Key in Cookie)) {
Return
}
Delete a specified cookie from an internally maintained cookie group
Delete Cookie[key];

Also delete the name of the cookie in the internal array
for (var i=0; i < keys.length; i++) {
if (keys[i] = = = key) {
Keys.splice (i, 1);
Break
}
}
This.length--;
Delete the specified cookie by setting the cookie value to an empty string and setting the validity period to zero
Document.cookie = key + "=; Max-age=0 ";
};

Delete all Cookies
This.clear = function () {
Loop all the coolie names and delete the cookies
for (var i = 0; i < keys.length; i++)
Document.cookie = Keys[i] + "=; Max-age=0 ";
Reset all internal states
cookie = {};
key = [];
this.length = 0;

};
}

Recommended front-end Learning Group: HTML5/CSS3/JS/JQ/NODEJS/DIV Group number: 339840649

Cookie attributes, expiration and scope

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.