<! --
// 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 ",""));
-->