一、設置一二級域名共用的cookie:設置domain爲一級域名,可1、二級域名共用的cookiejavascript
$.cookie('f_city','北京|101010100|,錦州|101070701|',{expires: 999, path: '/',domain: 'weather.com.cn'})
二、刪除同名不一樣域的cookie (一個頁面中.weather.com.cn域 和 www.weather.com.cn域下,都有f_city這個值,怎麼刪除其中任一)php
$.cookie('f_city',null,{ path: '/'}) //刪除當前域 www.weather.com.cn域下的 f_city $.cookie('f_city',null,{ path: '/', domain: 'weather.com.cn'}) //刪除 .weather.com.cn域下的 f_city
三、讀取cookie:$.cookie('f_city'),當讀取cookie鍵值爲 f_city 的值時,若是有不一樣域下的多個f_city,則讀取建立最先的f_city值,而與域無關html
var cookie_f_city= $.cookie('f_city'); //讀取cookie只有一種寫法,不接受{domain}參數,也就是說,當有域同名參數時,只讀建立最先的 f_city
=======================java
下面是jq-cookie這個插件的源碼jquery
/** * 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 * */ /** * *一、設置cookie的值,好比咱們要設置變量名爲userid對應值爲123的cookie,代碼以下: * $.cookie('userid','123'); * *二、新建一個cookie,並設置cookie的有效期 路徑 域名等,代碼以下: * $.cookie('userid, '123', {expires: 7, path: '/', domain: 'jquery.com', secure: true});注意:若是去掉後面{}的參數,新建後將以默認設置生效。 * *三、刪除cookie,即把對應cookie值置爲null,代碼以下: * $.cookie('userid', null); * *四、讀取cookie,如讀取變量名爲userid的cookie值,代碼以下: * var uId= $.cookie('userid'); * */ /** * Get the value of a cookie with the given name. * * @example $.cookie('the_cookie'); * @desc Get the value of a cookie. * * @param String name The name of the cookie. * @return The value of the cookie. * @type String * * @name $.cookie * @cat Plugins/Cookie * @author Klaus Hartl/klaus.hartl@stilbuero.de */ jQuery.cookie = 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? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } };
var cookie_f_city= $.cookie('f_city'); //讀取cookie只有一種寫法,不接受{domain}參數,也就是說,當有域同名參數時,只讀建立最先的 f_city
cookie