<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
<Type = "text/JavaScript"> var divy = 0 // to achieve the ID card, move the mouse pointer to the main drive to browse the created Variable Function mailtodxx () {mailstr = prompt ('Enter the name of the member who receives the short message. The default title is the title of this page. The default content is the URL of this page ', ''); If (mailstr! = ''& Mailstr! = NULL) {window. Open ('mailtodxx. asp? Submit = '+ document. Title +' & Body = '+ location. href +' & to = '+ mailstr, 'dxxwindow', 'top = 22000 ')}}