Set up cookies and get cookies

Source: Internet
Author: User

/**
* Created by admin on 2016/3/30.
*/

function Setcookie (name,value,daytolive) {
var cookie = name + "=" +encodeuricomponent (value);
if (typeof daytolive = = = = "Number") {
Cookie + = "; max-age=" + (daytolive * 24 * 60 * 60);

}
Document.cookie = cookie;
}

function GetCookies () {
var cookies = {};
var all = Document.cookie;
if (all = = = "") {
return cookies;
}

var list = All.split (";");
for (Var i=0,len=list.length;i<len;i++) {
var cookie = list[i];
var p = cookie.indexof ("=");
var name = cookie.substring (0,p);
var value = decodeuricomponent (cookie.substring (p+1));
Cookies[name] = value;
}
return cookies;

}

Setcookie ("AAA", "111", 5);
Setcookie ("BBB", "222", 5);

var cookies = getcookies ();
for (var i in cookies) {

Attention!!! Firefox will default to two more private properties, but all start with "_"
if (Cookies.hasownproperty (i) && I.indexof ("_")! = 0) {
Document.cookie = i+ "=0;expires=" +new Date (0). toUTCString ();
Alert (i+ ":" + cookies[i]);
}
}

When you write the cache separately for IE, you can use the scope of the IE Userdata,ie UserData in the document that is in the same directory as the current document





Set up cookies and get cookies

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.