Javascript operation cookie

Source: Internet
Author: User

<! --
// Learn how to use js to operate cookies

// Write cookie
// Case insensitive for cookiename
// Do not consider the subkey
Function writecookies (cookiename, cookievalue, expires)
{
If (expires)
{
// Expires is specified.
Document. Cookie =
Writecookies_getcookiename (cookiename) + "=" + escape (cookievalue)
+ "; Expires =" + xpires. togmtstring ();
}
Else
{
Document. Cookie = writecookies_getcookiename (cookiename) + "=" + escape (cookievalue );
}
}

// Obtain and return the cookie name with the same name as cookiename, which must be case-insensitive.
// If such a cookie does not exist, cookiename is returned.
Function writecookies_getcookiename (cookiename)
{
VaR lowercookiename = cookiename. tolowercase ();
VaR cookiestr = Document. Cookie;

If (cookiestr = "")
{
Return cookiename;
}

VaR cookiearr = cookiestr. Split (";");
VaR Pos =-1;
For (VAR I = 0; I <cookiearr. length; I ++)
{
Pos = cookiearr [I]. indexof ("= ");
If (Pos> 0)
{
If (cookiearr [I]. substring (0, POS). tolowercase () = lowercookiename)
{
Return cookiearr [I]. substring (0, POS );
}
}
}

Return cookiename;
}

// Obtain and return the cookie value
// Case insensitive for cookiename
// Dfltvalue is the default Return Value
// Do not consider the subkey
Function readcookies (cookiename, dfltvalue)
{
VaR lowercookiename = cookiename. tolowercase ();
VaR cookiestr = Document. Cookie;

If (cookiestr = "")
{
Return dfltvalue;
}

VaR cookiearr = cookiestr. Split (";");
VaR Pos =-1;
For (VAR I = 0; I <cookiearr. length; I ++)
{
Pos = cookiearr [I]. indexof ("= ");
If (Pos> 0)
{
If (cookiearr [I]. substring (0, POS). tolowercase () = lowercookiename)
{
Return Unescape (cookiearr [I]. substring (Pos + 1, cookiearr [I]. Length ));
}
}
}

Return dfltvalue;
}


// Test all the records

Document. Write ("Write cookie named CV... <br> ");
Writecookies ("test", "test123", null );

Document. Write ("write name is AB, cookie with expiration date... <br> ");
VaR expires = new date ("December 11,201 0 ");
Writecookies ("singleblue", "test234", expires );

Document. Write ("read cookies named singleblue..." + readcookies ("AB ",""));
-->

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.