Cookies are small text files placed on the client by website designers. Cookies can provide many benefits to users. For example, shopping websites store product lists that users have browsed, or portal websites remember which types of news users like to browse. With the user's permission, the user's login information can also be stored so that the user does not have to type this information every time they visit the website
Usage:
1.Introduce jquery.cookie.js
<script src="scripts/jquery-1.6.4.js" type="text/javascript"></script> <script src="scripts/jquery.cookie.js" type="text/javascript"></script>
2. Method
1). Add a new session cookie: $.cookie('the_cookie', 'the_value');
Note: When the cookie validity period is not specified, the created cookie will be valid until the user closes the browser by default, so it is called "session cookie".
2). Create a cookie and set the validity period to 7 days: $.cookie('the_cookie', 'the_value', { expires: 7 });
Note: When the cookie validity period is specified, the cookie created is called "persistent cookie (persistent cookie) ".
3). Create a cookie and set the effective path of the cookie: $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
Note: By default, only the web page that sets the cookie can read the cookie. If you want a page to read the cookie set by another page, you must set the cookie path. The path to the cookie is used to set the top-level directory that can read the cookie. Setting this path as the root directory of the website allows all web pages to read each other's cookies (generally do not set this to prevent conflicts).
4). Read cookie:
$.cookie('the_cookie'); // cookie存在 => 'the_value' $.cookie('not_existing'); // cookie不存在 => null
$.cookie('the_cookie', null);
3. Write cookie to file
var COOKIE_NAME = 'username'; if( $.cookie(COOKIE_NAME) ){ $("#username").val( $.cookie(COOKIE_NAME) ); } $("#check").click(function(){ if(this.checked){ $.cookie(COOKIE_NAME, $("#username").val() , { path: '/', expires: 10 }); //var date = new Date(); //date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000)); //三天后的这个时候过期 //$.cookie(COOKIE_NAME, $("#username").val(), { path: '/', expires: date }); }else{ $.cookie(COOKIE_NAME, null, { path: '/' }); //删除cookie } });
expires: (Number | Date): Validity period, you can set an integer as the validity period (unit: days), or you can set a date object as the expiration date of the cookie. If the specified date is a negative number, then this cookie will be deleted; if not set or set to null, then this cookie will be treated as a Session Cookie and deleted after the browser is closed
path: (String): Cookie’s path attribute, the default is the page path where the cookie is created
domain: (String): The domain name attribute of the cookie. The default is the domain name of the page that created the cookie
secure: (Boolean) : If set to true, the transmission of this cookie will require a secure protocol, such as HTTPS.
The above is a summary of how to use the Cookie plug-in from several articles compiled by the editor. I hope it can help everyone.