Store, retrieve, and delete JavaScript cookies.

Source: Internet
Author: User

<Script>

// Author of the cookie writing function: Yan zhenkai
Function SetCookie (name, value) // two parameters: one is the name of the cookie and the other is the value.
{
Var Days = 30; // This cookie will be saved for 30 Days
Var exp = new Date (); // new Date ("December 31,999 8 ");
Exp. setTime (exp. getTime () + Days x 24x60*60*1000 );
Document. cookie = name + "=" + escape (value) + "; expires =" + exp. toGMTString ();
}
Function getCookie (name) // The cookie function.
{
Var arr = document. cookie. match (new RegExp ("(^ |)" + name + "= ([^;] *) (; | $ )"));
If (arr! = Null) return unescape (arr [2]); return null;

}
Function delCookie (name) // delete a cookie
{
VaR exp = new date ();
Exp. settime (exp. gettime ()-1 );
VaR cval = getcookie (name );
If (cval! = NULL) document. Cookie = Name + "=" + cval + "; expires =" + exp. togmtstring ();
}

Setcookie ("Xiaoqi", "3 ")
Alert (getcookie ('xiaoqi '));
</SCRIPT>

A very practical JavaScript function for reading and writing cookies

A very practical JavaScript function for reading and writing cookies
Function getcookieval (offset)
// Obtain the decoded cookie value
{
VaR endstr = documents. Cookie. indexof (";", offset );
If (endstr =-1)
Endstr = documents. Cookie. length;
Return Unescape (events. Cookie. substring (offset, endstr ));
}
Function SetCookie (name, value)
// Set the Cookie value
{
Var expdate = new Date ();
Var argv = SetCookie. arguments;
Var argc = SetCookie. arguments. length;
Var expires = (argc> 2 )? Argv [2]: null;
Var path = (argc> 3 )? Argv [3]: null;
Var domain = (argc> 4 )? Argv [4]: null;
Var secure = (argc> 5 )? Argv [5]: false;
If (expires! = Null) expdate. setTime (expdate. getTime () + (expires * 1000 ));
Events. cookie = name + "=" + escape (value) + (expires = null )? "": ("; Expires =" + expdate. toGMTString ()))
+ (Path = null )? "": ("; Path =" + path) + (domain = null )? "": ("; Domain =" + domain ))
+ (Secure = true )? "; Secure ":"");
}
Function DelCookie (name)
// Delete the Cookie
{
Var exp = new Date ();
Exp. setTime (exp. getTime ()-1 );
Var cval = GetCookie (name );
Documents. cookie = name + "=" + cval + "; expires =" + exp. toGMTString ();
}
Function GetCookie (name)
// Obtain the original Cookie value
{
Var arg = name + "= ";
Var alen = arg. length;
Var clen = documents. cookie. length;
Var I = 0;
While (I <clen)
{
Var j = I + alen;
If (documents. cookie. substring (I, j) = arg)
Return GetCookieVal (j );
I = documents. cookie. indexOf ("", I) + 1;
If (I = 0) break;
}
Return null;
}

<Script language = "JavaScript">
<! --
Function openpopup (){
Url = "popup.htm"
Window. Open ("gonggao.htm", "gonggao", "width = 260, Height = 212, Left = 200, Top = 0 ")
}

Function get_cookie (name ){
VaR search = Name + "="
VaR returnvalue = "";
If (events. Cookie. length> 0 ){
Offset = documents. Cookie. indexof (Search)
If (offset! =-1 ){
Offset + = search. Length
End = documents. Cookie. indexof (";", offset );
If (END =-1)
End = documents. Cookie. length;
Returnvalue = unescape (events. cookie. substring (offset, end ))
}
}
Return returnvalue;
}

Function helpor_net (){
If (get_cookie ('popped') = ''){
Openpopup ()
Documents. cookie = "popped = yes"
}
}
Helpor_net ()
// -->
</SCRIPT>

If you click OK, as long as the cookie is unclear, the system will not prompt for future access. If you do not click OK, the system will prompt every time. Put it in the js file, full site inclusion

<Script language = "JavaScript">
<! --
Var the_cookie = document. cookie;
Var broken_cookie = the_cookie.split (":");
Var the_visiteraccepted = unescape (broken_cookie [1]);
//
If (the_visiteraccepted = "undefined "){
VaR TMP = confirm ('when and where are Chinese people. ');
If (TMP = false ){
Window. Close ();
} Else {
VaR the_visiteraccepted = 1;
VaR the_cookie = "ilovechina = visiteraccepted:" + escape (the_visiteraccepted );
Document. Cookie = the_cookie;
}
}
// -->
</SCRIPT>

1. Cookie compatibility

The Cookie format has two different versions. The first Version, called Cookie Version 0, was originally developed by Netscape and is supported by almost all browsers. The newer Version, Cookie Version 1, is developed according to RFC 2109. To ensure compatibility, JAVA stipulates that all the Cookie-related operations mentioned earlier are performed on old versions of cookies. The new Cookie version is not supported by the Javax. servlet. http. Cookie package.

2. Cookie content

The character restrictions of the same Cookie content vary with different Cookie versions. In Cookie Version 0, some special characters, such as space, square brackets, Parentheses, equal signs (=), commas, double quotation marks, slashes, question marks, @ symbols, colons, the semicolon cannot be used as the Cookie content. This is why we set the Cookie content to "Test_Content" in the example.

Although these characters can be used in Cookie Version 1, the new Cookie specification is not currently supported by all browsers. Therefore, it is safe, we should avoid using these characters in Cookie content

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/zxmcl/archive/2007/08/02/1723595.aspx

Related Article

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.