How to use jQuery to operate Cookies method resolution

  • 2021-08-16 22:51:16
  • OfStack

Cookies

Definition: Let the website server store a small amount of data to the client's hard disk or memory, and read data from the client's hard disk;

Download and import: jquery. cookie. js is based on jquery;; Introduce jquery first, and then introduce jquery. cookie. js; Download: http://plugins.jquery.com/cookie/

< script type="text/javascript" src="js/jquery.min.js" >
< /script > < script type="text/javascript" src="js/jquery.cookie.js" > < /script >

jquery. cookie. js code content is not much, you can directly copy under 1


jQuery.cookie = function (key, value, options) {

  // key and value given, set cookie...
  if (arguments.length > 1 && (value === null || typeof value !== "object")) {
    options = jQuery.extend({}, options);

    if (value === null) {
      options.expires = -1;
    }

    if (typeof options.expires === 'number') {
      var days = options.expires, t = options.expires = new Date();
      t.setDate(t.getDate() + days);
    }

    return (document.cookie = [
      encodeURIComponent(key), '=',
      options.raw ? String(value) : encodeURIComponent(String(value)),
      options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
      options.path ? '; path=' + options.path : '',
      options.domain ? '; domain=' + options.domain : '',
      options.secure ? '; secure' : ''
    ].join(''));
  }

  // key and possibly options given, get cookie...
  options = value || {};
  var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
  return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

1. Add 1 "Session cookie"

$.cookie('the_cookie', 'the_value');

The valid time of cookie is not specified here, and the created cookie is valid until the user closes the browser by default, so it is called "Session cookie (session cookie)".

2. Create an cookie and set the valid time to 7 days

$.cookie('the_cookie', 'the_value', { expires: 7 });

The effective time of cookie is specified here, and the created cookie is called "persistent cookie (persistent cookie)". Note that the unit is: days;

PS: There seems to be a problem here. After trying for half a day, it is found that the browser will be invalid when the cookie set by jquery is closed. https://www.cnblogs.com/acm-bingzi/p/jquery _ cookie _ expire. html

3. Create an cookie and set a valid path to cookie

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

By default, cookie can only be read from a Web page where cookie is set. If you want one page to read the cookie set by another page, you must set the path of cookie. The path to cookie is used to set the top-level directory where cookie can be read. By setting this path to the root directory of the website, all web pages can read cookie from each other (1. Do not set this path to prevent conflicts).

4. Read cookie

$.cookie('the_cookie');

5. Delete cookie

$.cookie('the_cookie', null); //通过传递null作为cookie的值即可

6. Optional parameters

$.cookie('the_cookie','the_value',{
expires: 7,
path: '/',
domain: 'jquery. com',
secure: true
})

expires: (NumberDate) expiry date; When 1 integer is set, the unit is days; You can also set a date object as the expiration date of Cookie; path: (String) Create the page path of the Cookie; domain: (String) Create the page domain name of the Cookie; secure: (Booblean) If it is set to true, the transmission of this Cookie will require a security protocol, such as HTTPS;

Related articles: