Home > Web Front-end > JS Tutorial > body text

jQuery gets cookie value and deletes cookie usage example_jquery

WBOY
Release: 2016-05-16 15:05:22
Original
1614 people have browsed it

The example in this article describes the usage of jQuery to obtain cookie value and delete cookie. Share it with everyone for your reference, the details are as follows:

Cookie has a designated cookie operation class in jquery. Here we will first introduce some problems when using cookie operation classes, and then introduce the correct usage method.

When using JQuery to operate cookies, an incorrect value occurs:

It turns out that cookies have four different attributes:

Name, content, domain, path

$.cookie('the_cookie'); // 读取 cookie
$.cookie('the_cookie', 'the_value'); // 存储 cookie
$.cookie('the_cookie', 'the_value', { expires: 7 }); // 存储一个带7天期限的 cookie
$.cookie('the_cookie', '', { expires: -1 }); // 删除 cookie

Copy after login

Use:

$.cookie("currentMenuID", menuID);

Copy after login

When the domain and path are not specified.

So different cookies will be generated when the domain and path are different

$.cookie("currentMenuID");

Copy after login

There will be problems when getting the value.

Therefore, use:

$.cookie("currentMenuID", "menuID", { path: "/"});

Copy after login

Override. The same cookieID in the same domain corresponds to a value.

Let’s take a look at an example

Note about the path setting of cookies. If you do not set path:'/', the path will be automatically set according to the directory (such as: http://www.xxx.com/user/, the path will be set to ' /user')

$.extend({
/**
 1. 设置cookie的值,把name变量的值设为value
example $.cookie('name', 'value');
 2.新建一个cookie 包括有效期 路径 域名等
example $.cookie('name', 'value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
3.新建cookie
example $.cookie('name', 'value');
4.删除一个cookie
example $.cookie('name', null);
5.取一个cookie(name)值给myvar
var account= $.cookie('name');
**/
  cookieHelper: function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
      options = options || {};
      if (value === null) {
        value = '';
        options.expires = -1;
      }
      var expires = '';
      if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
        var date;
        if (typeof options.expires == 'number') {
          date = new Date();
          date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
        } else {
          date = options.expires;
        }
        expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
      }
      var path = options.path ? '; path=' + options.path : '';
      var domain = options.domain ? '; domain=' + options.domain : '';
      var secure = options.secure ? '; secure' : '';
      document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
      var cookieValue = null;
      if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
          var cookie = jQuery.trim(cookies[i]);
          // Does this cookie string begin with the name we want&#63;
          if (cookie.substring(0, name.length + 1) == (name + '=')) {
            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
            break;
          }
        }
      }
      return cookieValue;
    }
  }
});

Copy after login

Readers who are interested in more jQuery-related content can check out the special topics on this site: "JQuery cookie operation skills summary", "jQuery table (table) operation skills summary" , "Summary of jQuery drag effects and techniques", "Summary of jQuery extension techniques", "Summary of jQuery common classic special effects", "jQuery animation and special effects usage summary", "jquery selector usage summary" and "jQuery common plug-ins and usage summary"

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template