最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery獲取cookie值及刪除cookie用法實例

 更新時間:2016年04月15日 09:44:04   作者:php100  
這篇文章主要介紹了jQuery獲取cookie值及刪除cookie用法,實例分析了jQuery操作cookie時域和路徑的作用,以及針對cookie的讀取與刪除技巧,需要的朋友可以參考下

本文實例講述了jQuery獲取cookie值及刪除cookie用法。分享給大家供大家參考,具體如下:

cookie在jquery中有指定的cookie操作類,這里先來介紹在使用cookie操作類時的一些問題,然后介紹正確的使用方法。

使用JQuery操作cookie時 發(fā)生取的值不正確的問題:

結(jié)果發(fā)現(xiàn)cookie有四個不同的屬性:

名稱,內(nèi)容,域,路徑

$.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

使用:

$.cookie("currentMenuID", menuID);

時 未指定域和路徑。

所以當域和路徑不同時會產(chǎn)生不同的cookie

$.cookie("currentMenuID");

取值時會產(chǎn)生問題。

因此,使用:

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

進行覆蓋。同域下同一個cookieID對應一個值。

下面我們來看個實例

關(guān)于cookie的path設(shè)置需要注意,如果不設(shè)置path:'/'的話,path則會根據(jù)目錄自動設(shè)置(如:http://www.xxx.com/user/,path會被設(shè)置為 '/user')

$.extend({
/**
 1. 設(shè)置cookie的值,把name變量的值設(shè)為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?
          if (cookie.substring(0, name.length + 1) == (name + '=')) {
            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
            break;
          }
        }
      }
      return cookieValue;
    }
  }
});

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery的cookie操作技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jQuery常見經(jīng)典特效匯總》、《jQuery動畫與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)

希望本文所述對大家jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論

乌拉特中旗| 尉氏县| 台山市| 乡城县| 徐汇区| 商都县| 乌拉特后旗| 富裕县| 登封市| 礼泉县| 城口县| 青岛市| 抚顺县| 台中市| 嵩明县| 临颍县| 黄骅市| 永仁县| 荣昌县| 黑河市| 景宁| 连平县| 彰武县| 荔波县| 莱芜市| 富平县| 东阳市| 普安县| 尚志市| 屯昌县| 房产| 福鼎市| 佛坪县| 亚东县| 分宜县| 偃师市| 烟台市| 定安县| 南召县| 辽宁省| 平乐县|