1, set up a one or two-level domain name sharing cookie: Set domain as a first-level domain name, can secondary the domain name common cookie
$.cookie (' f_city ', ' Beijing |101010100|, Jinzhou |101070701| ', {expires:999, path: '/', Domain: ' weather.com.cn '})
2. Delete cookies with different domains of the same name (in one page. weather.com.cn and www.weather.com.cn domains, there are f_city this value, how to delete any of them)
$.cookie (' f_city ', null,{path: '/'}) //Delete the F_city$.cookie under the current domain www.weather.com.cn domain (' f_city ', null,{path: '/', Domain: ' weather.com.cn '}) //delete. f_city under weather.com.cn domain
3. Read Cookies: $.cookie (' f_city '), when reading the value of a cookie key value of f_city, if there are multiple f_city under different domains, read the first f_city value created, and not domain-independent
var cookie_f_city= $.cookie (' f_city '); The read cookie has only one notation and does not accept the {domain} parameter, that is, read-only creation of the earliest f_city when there are parameters with the same name as the domain
=======================
Below is the source code of Jq-cookie this plugin
/** * Cookie Plugin * * Copyright (c) 2006 Klaus Hartl (stilbuero.de) * Dual licensed under the MIT and GPL licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html **//** * * 1, set the value of the cookie, for example, we want to set the variable name userid corresponding to a value of 123 cookie, the code is as follows: * $.cookie (' userid ', ' 123 '); * * 2, create a new cookie, and set the validity of the cookie path domain name, and so on, the code is as follows: * $.cookie (' userid, ' 123 ', {expires:7, path: '/', Domain: ' jquery.com ', secure:true}); Note: If you remove Parameters for the {} later, which will take effect with the default settings after new. * * 3, delete the cookie, the corresponding cookie value is set to NULL, the code is as follows: * $.cookie (' userid ', null); * * * *, read the cookie, such as read the variable name UserID of the cookie value, the code is as follows: * var uid= $.cookie (' userid '); * *//** Get the value of a cookie with the given name. * * @example $.cookie (' The_cookie '); * @desc get the value of a COO Kie. * * @param String Name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/cookie * @author Klaus hartl/[email protected]*/Jquery.cookie=function(name, value, options) {if(typeofValue! = ' undefined ') {//name and value given, set cookieoptions = Options | | {}; if(Value = = =NULL) {Value= ' '; Options.expires=-1; } varexpires = ' '; if(Options.expires && (typeofOptions.expires = = ' Number ' | |options.expires.toUTCString)) {vardate; if(typeofOptions.expires = = ' Number ') {Date=NewDate (); Date.settime (Date.gettime ()+ (Options.expires * 24 * 60 * 60 * 1000)); } Else{Date=Options.expires; } Expires= '; Expires= ' + date.toutcstring ();//Use expires attribute, Max-age isn't supported by IE } varPath = Options.path? ‘; Path= ' + options.path: '; varDomain = Options.domain? ‘; Domain= ' + options.domain: '; varSecure = options.secure? ‘; Secure ': '; Document.cookie= [Name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join ("); } Else{//Only name given, get cookie varCookievalue =NULL; if(Document.cookie && Document.cookie! = ") { varcookies = Document.cookie.split ('; ')); for(vari = 0; i < cookies.length; i++) { varCookie =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; } } } returnCookievalue; }};
var cookie_f_city= $.cookie (' f_city '); The read cookie has only one notation and does not accept the {domain} parameter, that is, read-only creation of the earliest f_city when there are parameters with the same name as the domain
Cookie Plugin |jq-cookie.js| Use