// Write the cookie function SetCookie (name, value) // two parameters, one is the cookie name 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 * 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 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 '));
The following is a very useful 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 Cookie {var exp = new Date (); exp. setTime (exp. getTime ()-1); var cval = GetCookie (name); events. 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 (documents.cookie.length > 0) {offset = documents.cookie.indexOf(search)if (offset != -1) {offset += search.lengthend = documents.cookie.indexOf(";", offset);if (end == -1)end = documents.cookie.length;returnvalue=unescape(documents.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, including the whole site.
<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>
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.
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.