Cookies
Definition: A technique that allows a Web server to store small amounts of data on the client's hard disk or memory, and to read data from the client's hard disk;
Download and introduction: Jquery.cookie.js based on jquery, first introduced jquery, then introduced: jquery.cookie.js; Download: http://plugins.jquery.com/cookie/
12 |
<script type= "text/javascript" src= "js/jquery.min.js" ></script> <script type= "text/javascript" src= "js/jquery.cookie.js" ></script> |
Use:
1. Add a "session cookie"
1 |
$.cookie( ‘the_cookie‘ , ‘the_value‘ ); |
There is no time specified for the cookie to be valid, and the cookie is created by default until the user closes the browser, so it is referred to as a "session cookie".
2. Create a cookie and set the validity time to 7 days
1 |
$.cookie( ‘the_cookie‘ , ‘the_value‘ , { expires: 7 }); |
This indicates the time the cookie is valid and the cookie created is referred to as a "persistent cookie (persistent cookie)". Note units are: days;
3. Create a cookie and set a valid path for the cookie
1 |
$.cookie( ‘the_cookie‘ , ‘the_value‘ , { expires: 7, path: ‘/‘ }); |
By default, only the Web page that sets the cookie can read the cookie. If you want a page to read a cookie set by another page, you must set the path to the cookie. The path to the cookie is used to set the top-level directory to read cookies. Set this path to the root of your Web site so that all Web pages can read cookies to each other (typically not set up to prevent collisions).
4. Read cookies
1 |
$.cookie( ‘the_cookie‘ ); |
5. Delete Cookies
1 |
$.cookie( ‘the_cookie‘ , null ); //通过传递null作为cookie的值即可 |
6. Optional Parameters
123456 |
$.cookie ( ' The_cookie ' The_value ' ,{ EXPIRES:7,&NBSP;&NBSP; path: '/' &NBSP;&NBSP;&NBSP;&NBSP; Code class= "JavaScript plain" >domain: ' jquery.com ' &NBSP;&NBSP;&NBSP;&NBSP; secure: true }) |
expires: (number| date); When an integer is set, the unit is days; You can also set a Date object as the expiration date of the cookie;
path: (String) creates the page path for the cookie;
domain: (String) The domain name of the page that created the cookie;
Secure: (Booblean) If set to true, then the transfer of this cookie will require a security protocol such as: HTTPS;
Jquery.cookie.js How to use