Quick Learning jquery Plug-in Cookie plugin use method _jquery

Source: Internet
Author: User

Cookies are small text files that Web designers place on the client. Cookies can provide users with a lot of things, such as a list of products that the shopping site stores users have visited, or a portal that remembers what kind of news users like to choose to browse. With the user's permission, you can also store the user's logon information so that users do not have to type the information each time they visit the site
How to use:
1. Introduction of 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. Methods

1). Add a new session cookie: $.cookie (' The_cookie ', ' the_value ');

Note: When a cookie is not valid, the validity of the cookie is created by default until the user closes the browser, so it is called "Session Cookie".

2. Create a cookie and set a valid time of 7 days: $.cookie (' The_cookie ', ' The_value ', {expires:7});

Note: When the cookie validity time is indicated, the cookie created is called " Persistent Cookie (persistent cookie)".

3. Create a cookie and set a valid path for 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 a cookie set by another page , you must set the path of the cookie. The path to the cookie is used to set up a top-level directory that can read cookies. Set this path to the root of the Web site so that all pages can read cookies to each other (typically do not set this up to prevent conflicts).

4). Read cookies:

$.cookie (' The_cookie '); Cookie exists => ' The_value '

$.cookie (' not_existing ');//cookie does not exist => null

5. Delete the cookie by passing null as the value of the cookie: $.cookie (' The_cookie ', null);

3. Write cookies 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)); 
    Three days later this time expires//$.cookie (Cookie_name, $ ("#username"). Val (), {path: '/', expires:date}); 
}else{$.cookie (cookie_name, NULL, {path: '/'});//Delete Cookie}}); 

Parameter settings:
Expires: (number | Date: The validity period, you can set an integer as the validity period (in days), or you can set a Date object as the expiration date for the cookie. If the specified date is a negative number, the cookie is deleted, and if it is not set or set to NULL, the cookie is treated as a session cookie and is deleted after the browser is closed
path: (String): The path property of the cookie, which defaults to the path of the page where the cookie is created
domain: (String): The domain Name property of the cookie, which is the default page name for creating the cookie
Secure: (Boolean) : If set to true, the transmission of this cookie will require a security protocol, such as HTTPS.

The above is a small compilation of several articles synthesized on the use of cookie plug-ins, hope can help everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.