Detailed description of Cookie operations (setting, reading, and deleting) using javascript, and javascriptcookie

Source: Internet
Author: User
Tags set cookie

Detailed description of Cookie operations (setting, reading, and deleting) using javascript, and javascriptcookie

Cookie is a way for the client to store data and can be used for status persistence.

1. Set Cookie:

A. No expiration time: (if no expiration time is set, the default session-level Cookie will expire if the browser is closed)

Copy codeThe Code is as follows:
Function setCookie (name, value ){
Document. cookie = name + '=' + escape (value );
}

B. fixed expiration time:

Copy codeThe Code is as follows:
Function setCookie (name, value)
{
Var Days = 30;
Var exp = new Date ();
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}

C. Custom expiration time:

Copy codeThe Code is as follows:
// Set the custom expiration time cookie
Function setCookie (name, value, time)
{
Var msec = getMsec (time); // get millisecond
Var exp = new Date ();
Exp. setTime (exp. getTime () + msec * 1 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
// Converts string time to millisecond, 1 second = 1000 milliseconds
Function getMsec (DateStr)
{
Var timeNum = str. substring (0, str. length-1) * 1; // time quantity
Var timeStr = str. substring (str. length-1, str. length); // time unit prefix. For example, h indicates the hour.

If (timeStr = "s") // 20 s indicates 20 seconds
{
Return timeNum * 1000;
}
Else if (timeStr = "h") // 12 h indicates 12 hours
{
Return timeNum * 60x60*1000;
}
Else if (timeStr = "d ")
{
Return timeNum * 24*60*60*1000; // 30d indicates 30 days
}
}

2. Read Cookie:

Copy codeThe Code is as follows:
Function getCookie (name)
{
Var arr, reg = new RegExp ("(^ |)" + name + "= ([^;] *) (; | $)"); // Regular Expression matching
If (arr = document. cookie. match (reg )){
Return unescape (arr [2]);
}
Else {
Return null;
}
}

3. delete a Cookie:

Copy codeThe Code is as follows:
Function delCookie (name)
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = getCookie (name );
If (cval! = Null ){
Document. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
}

4. Call example:

Copy codeThe Code is as follows:
SetCookie ("name", "hayden ");
Alert (getCookie ("name "));

The above is all about the javascript operation cookie in this article. I hope it will help you learn javascript.

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.