Cookies, translated into Chinese as cookies, are just a small piece of information in the browser cache,Website productionUsers can be identified, such as whether the monthly user has visited for the first time, what content has been browsed, etc.It can also be used to verify whether the user logs in to the website, so that the user can log in to the website only once, and does not need to log in again the next time.In short, cookies are very useful.JavaScript can easily set, obtain and delete cookies. Check the application case of code 1-9.
Save the above code as "cookie. htm", open it with a browser, and the result is as shown in Figure 1.20.
Code analysis: In the JavaScript footer of the above example, there are structures and key words that have never been seen before.
The function that starts with the keyword "function" is called a function, followed by the function name "setCookie", and the parameter name, value, and days are enclosed in parentheses after the function name.There are three such functions in this code. These three functions are not run immediately after they are defined. For example, the "setCookie" function is run only when it is called in line 31 of the code. There is no "function" keyword when calling the function.At the same time, the parameters after the function name are replaced with the values expected to be passed to the function. This is because the function is like a machine that can be used only after it is built. Lines 7-26 are used to build the function, and lines 31, 34, 37, and 40 are used to use the function.The setCookie function in line 31 sets a cookie named "dan" with a value of "3333 'and a validity period of" 60 "days. In line 34, the number of getCookies gets the cookie value named" dan ". If there is a value, the set value will be displayed. If there is no cookie named" dan "," null "will be displayed. In line 37, the delCookie function deletes the cookie named" dan ".In line 40, the getCookie function is called again to display the value of "dan". At this time, "null" is displayed because the cookie named "dan" has been deleted.