jQuery plugin - Cookie plug jquery.cookie.js

jQuery Cookie Download: https://codeload.github.com/carhartl/jquery-cookie/legacy.zip/v1.4.1

1. The 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. Use

1) add a new session cookie:
$.cookie('The_cookie', 'the_value');
Note: When there is no effective time specified cookie, cookie created valid user closes the browser defaults to date, it is called "Session cookie (session cookie)".
2) create a cookie and set the effective time of 7 days:
$.cookie('The_cookie', 'the_value', {expires: 7});
Note: When the effective time specified in the cookie, cookie created is called "persistent cookie (persistent cookie)".
3) create a cookie and set the cookie effective path:
$.cookie('The_cookie', 'the_value', {expires: 7, path: '/'});
NOTE: By default, only the set cookie page in order to read the cookie. If you want to read one page to another page set
Set of cookie, you must set a cookie path. cookie path is used to set the cookie can read the top-level directory. This
A path to the root directory of your site, you can make all the pages can be read each cookie (usually not so set up to prevent conflicts).
4) read cookie:
$.cookie('The_cookie'); // cookie exists => 'the_value'

$.cookie('Not_existing'); // cookie does not exist => null
5) delete cookie, cookie by passing null as the value to:
$.cookie('The_cookie', null);

3. write the 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, domain: 'jquery.com', secure: true}.);
            // Var date = new Date();
            //date.setTime (date.getTime () + (3 * 24 * 60 * 60 * 1000)); // this time expires after three days
           //$.cookie(COOKIE_NAME, $ ("# username")val(), {path: '/', expires: date});.
         } Else {
           $.cookie(COOKIE_NAME, null, {path: '/'}); // delete the cookie
        }
      });
Parameter settings:
expires: (Number | Date) is valid, you can set an integer as valid (unit: days), you can also set a date object as Cookie expiration date. If the specified date is negative, then the cookie will be deleted; if not set or set to null, then the cookie will be treated as Session Cookie processing and deleted after the browser is closed
path: (String) Cookie path attribute, the default is to create a page that the cookie path
domain: (String) Cookie domain property, the default is to create a page that the cookie domain
secure: (Boolean) If true, then the cookie will require a secure transmission protocol, such as HTTPS

Pingbacks are closed.

Trackbacks

Comments are closed.