The following small series will bring you an early understanding and application of cookies (js and jq ). I think it is quite good. Now I will share it with you and give you a reference. Let's take a look at the small Editor and wish you a pleasant game.
What is a cookie?
---------------------------------------------------
Cookie is a mechanism provided by the browser. It provides the cookie attribute of the document Object to JavaScript. It can be controlled by JavaScript, rather than JavaScript itself. Cookie is a file stored on the user's hard disk. This file usually corresponds to a domain name. When the browser accesses this domain name again, the cookie is available. Therefore, cookies can span multiple webpages under a domain name, but cannot be used across multiple domain names.
Cookie usage
---------------------------------------------------
(1) Save the user logon status. For example, if you store a user ID in a cookie, you do not need to log on again when you access the page the next time. Currently, many forums and communities provide this function. Cookie can also set the expiration time. When the expiration time is exceeded, the cookie will automatically disappear. Therefore, the system often prompts users to stay logged on for a period of time. Common options include one month, three months, and one year.
(2) tracking user behavior. For example, a weather forecast website can display local weather conditions based on the selected area. If you need to select the location every time, it will be cumbersome. After using cookies, it will become very user-friendly. The system can remember the last accessed area. When you open this page next time, it automatically displays the weather conditions of the region where the last user was located. Because everything is done on the back end, such a page is just as customized for a user, which is very convenient to use.
(3) custom page. If the website supports skin replacement or layout replacement, you can use cookies to record user options, such as background color and resolution. You can save the interface style of the last access when you access it next time.
Cookie usage
---------------------------------------------------
Js mode:
Function setCookie (sName, sValue, oExpires, sPath, sDomain, bSecure) {// js sets cookie var sCookie = sName + '=' + encodeURIComponent (sValue); if (oExpires) {var date = new Date (); date. setTime (date. getTime () + oExpires * 60x60*1000); sCookie + = '; expires =' + date. toUTCString ();} if (sPath) {sCookie + = '; path =' + sPath; if (sDomain) {sCookie + = '; domain =' + sDomain ;} if (bSecure) {sCookie + = '; secure';} document. cookie = sCookie ;}
Function getCookie (name) {// obtain the cookie var strCookie = document. cookie; var arrCookie = strCookie. split (";"); for (var I = 0; IFunction delCookie (name) {// Delete cookie // This function checks whether the cookie is set. If so, the expiration time is adjusted to the past time; // leave the remaining time to the operating system to clear the cookie. if (getCookie (name) {document. cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT ";}}
Jq plugin method:
Jq official website http://plugins.jquery.com/search cookieplug-in, a few kits, very convenient to use: