This article mainly introduces jQuery's usage of obtaining cookie values and deleting cookies. It analyzes the functions of jQuery in operating cookie time domains and paths, as well as its reading and deleting skills for cookies, for more information about how to use jQuery to obtain and delete a cookie, see the following example. We will share this with you for your reference. The details are as follows:
Cookie has a specified cookie operation class in jquery. Here we will first introduce some problems when using the cookie operation class, and then introduce the correct usage methods.
The following error occurs when you use JQuery to perform cookie operations:
The cookie has four different attributes:
Name, content, domain, path
$. Cookie (the _ cookie); // read cookie $. cookie (the _ cookie, the _ value); // store the cookie $. cookie (the _ cookie, the _ value, {expires: 7}); // store a cookie with a 7-day validity period $. cookie ('the _ cookies', '', {expires:-1}); // delete a cookie
Usage:
$.cookie("currentMenuID", menuID);
The domain and path are not specified.
Therefore, different cookies are generated when the domain and path are different.
$.cookie("currentMenuID");
An error occurs when the value is set.
Therefore, use:
$.cookie("currentMenuID", "menuID", { path: "/"});
. The same cookieID in the same domain corresponds to a value.
Let's look at an instance.
Note that if path: '/' is not set, the path will be automatically set according to the directory (for example, http://www.xxx.com/user/, and pathwill be set to '/user ')
$. Extend ({/** 1. set the cookie value to valueexample $. cookie ('name', 'value'); 2. create a cookie, including the Domain Name of the validity period and other example $. cookie ('name', 'value', {expires: 7, path: '/', domain: 'jquery. com ', secure: true}); 3. create cookieexample $. cookie ('name', 'value'); 4. delete A cookieexample $. cookie ('name', null); 5. get a cookie (name) value to myvarvar 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 {d Ate = 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? If (cookie. substring (0, name. length + 1) = (name + '=') {cookieValue = decodeURIComponent (cookie. substring (name. length + 1); break ;}}return cookieValue ;}}});