English
<!-- Add the following code to the <body> area--> <SCRIPT language=javascript> <!--// //Function: write cookie function WriteCookie (cookieName, cookieValue, expiry) { var expDate = new Date (); if(expiry) //If the cookie expiration time is set; { expDate.setTime (expDate.getTime() + expiry); document.cookie = cookieName + "=" + escape (cookieValue) + "; expires=" + expDate.toGMTString(); } else //The cookie expiration time is not set; { document.cookie = cookieName + "=" + escape (cookieValue); } } //Function: Get the value of the form form field as the related value of the cookie (cookie name, cookie value, expires) function setCookie () { var name = document.myform.text1.value; var value = document.myform.text2.value; var num = document.myform.text3.value; var select = document.myform.text3.selectedIndex; if (name=="" || value=="" || num==""){ alert ("Please enter the cookie name, value and expiration date before testing again!"); return false; } if(num == 0) { WriteCookie(name, value, 0); } else if(select == 0) //If the day is selected; time is converted into seconds; { WriteCookie(name, value, 1000 * 60 * 60 * 24 * num); } else if(select == 1) //If the selected month is month; { WriteCookie(name, value, 1000 * 60 * 60 * 24 * num * 31); } else if(select == 2) //If the year is selected; { WriteCookie(name, value, 1000 * 60 * 60 * 24 * num * 365); } alert ("Cookie has been saved, welcome to www.1stscript.com ") } //Function: read cookie value; function ReadCookie (CookieName) { var CookieString = document.cookie; var CookieSet = CookieString.split (';'); var SetSize = CookieSet.length; var CookiePieces var ReturnValue = "" ; var x = 0; for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) { CookiePieces = CookieSet[x].split ('='); if (CookiePieces[0 ].substring (0,1) == ' ') { CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length); } if (CookiePieces[0] == CookieName) { ReturnValue = CookiePieces[1]; } } alert ("Cookie Value is:"+ReturnValue); } //--> </SCRIPT> <TABLE align=center border=0 cellPadding=5 cellSpacing=0 width=550> <TBODY> <TR bgColor=#e7e7e7> <TD class=title height=17>What’s going on with cookies? </tr> <TR bgColor=#e7e7e7> <TD> <P> Client cookies can help us detect user status, such as whether the user has passed some special pages (for example: login page) before arriving at the current page, or the user has been Performed those operations. So: very widely used in shopping carts. For example, if a user purchases various products in different places, each time a product is purchased, the product can be temporarily stored in a cookie, and when the purchase is completed, the user can pay at the checkout. <P>Although Cookies have these benefits, their abuse is also very harmful. Malicious programs can obtain users' confidential information through Cookies and leak privacy rights. This is outside the scope of our discussion. Note: Do not write the user's account and password in the cookie, because the cookie is a text file in clear text, and anyone with access to the computer can see its content. Especially in public places such as "Internet cafes", don't forget to clear the cookies in the cache after use. </P></TD></TR> <TR> <TD bgColor=#ffcc99> <P>This example discusses how to set client Cookies</P></TD></TR> <TR> <TD > <FORM name=myform> <TABLE border=0 cellPadding=2 cellSpacing=0 width="95%"> <TBODY> <TR> <TD><SPAN class=e>Cookie name:</SPAN></TD > <TD><INPUT name=text1 value=javascript20000> </TD></TR> <TR> <TD><SPAN class=e>Cookie value:</SPAN></TD> <TD><INPUT name =text2 type=password></TD></TR> <TR> <TD><SPAN class=e>Cookie retention period:</SPAN></TD> <TD><INPUT name=text3 value=0> <SELECT name=Choice> <OPTION selected value=0>days</OPTION> <OPTION value=1>month</OPTION> <OPTION value=2>year</OPTION></SELECT> </TD></ TR> <TR> <TD colSpan=2><INPUT name=button2 onclick=setCookie() type=button value=Save Cookie> <INPUT name=button onclick="WriteCookie(document.myform.text1.value, '') ;alert('Cookies have been cleared!')" type=button value=Delete Cookie> <INPUT name=Submit onclick="ReadCookie (document.myform.text1.value)" type=submit value=View Cookie Content> </ TD></TR></TBODY></TABLE></FORM></TD></TR> </TBODY></TABLE>