What is a cookie
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Cookies are a mechanism provided by the browser that provides the cookie attribute of the document object to JavaScript. It can be controlled by JavaScript, not the nature of JavaScript itself. A cookie is a file stored on the user's hard disk. This file usually corresponds to a domain name. When the browser accesses the domain name again, the cookie is made available. Therefore, cookies can span multiple web pages under one domain name, but not multiple domain names.
Cookie usage occasion
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(1) Save the user's login status. For example, store the user ID in a cookie so that the user does not need to log in again the next time he visits the page. Now many forums and communities provide such functions. Cookies can also set the expiration time, and when the time period exceeds, the cookies will automatically disappear. Therefore, the system can often prompt the user to stay logged in for the time: common options include one month, three months, one year, etc.
(2) Track user behavior. For example, a weather forecast website can display local weather conditions according to the area selected by the user. It is tedious to choose the location every time, and when using cookies, it will appear very humanized. The system can remember the area you visited last time. When the page is opened next time, it will automatically display the weather conditions of the area where the user was located last time. Because everything is done in the background, such a page is as customized for a certain user and is very convenient to use.
(3) Customize the page. If the website provides the function of skinning or changing layout, you can use cookies to record user options, such as background color, resolution, etc. When the user visits the next time, he can still save the interface style of the last visit.
How to use cookies
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
js method:
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) { //js set cookie var sCookie = sName + '=' + encodeURIComponent(sValue); if (oExpires) { var date = new Date(); date.setTime(date.getTime() + oExpires * 60 * 60 * 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){ //Get cookie var strCookie=document.cookie; var arrCookie=strCookie.split("; "); for(var i=0;i<arrCookie.length;i++){ var arr=arrCookie[i].split("="); if(arr[0]==name){ return decodeURIComponent(arr[1]); } } return ""; } function delCookie(name){//Delete cookie// This function checks whether the cookie is set. If set, the expiration time will be adjusted to the past time;//The rest will be given to the operating system to clean up 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 for cookie plug-in, which is a size of several k, which is very convenient to use:
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
After introducing the above library files, the usage method is as follows:
<script> $.cookie('the_cookie'); //Read the cookie value $.cookie('the_cookie', 'the_value'); //Set the cookie value $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true}); // Create a new cookie including the valid path domain name, etc. $.cookie('the_cookie', 'the_value'); // Create a new cookie $.cookie('the_cookie', null); //Delete a cookie </script>The above first understanding and application of cookies (js and jq) is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.