成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? jQuery中cookie插件用法實例分析
全站頭部文字 我要出現在這里
jQuery中cookie插件用法實例分析 590 2024-03-18   

本文實例講述了jQuery中cookie插件用法。分享給大家供大家參考,具體如下:

Jquery里面提供了很多插件,方便,安全,快速實用。這節一起熟悉下JQUERY中操作Cookie的插件,插件名稱為jquery.cookie.js,可以去官方網站下載。

下載地址: http://plugins.jquery.com/project/cookie.

插件主要報告如下幾個用法

1.創建Cookie

創建一個名稱為username,值為 admin的cookie ,如下

$.cookie('username',"admin");

當然cookie還包括路徑,過期時間等其他參數,一起看下其他參數的用法
$.cookie('username', 'admin', {expires: 7, path: '/', domain: 'jquery.com', secure: true});

expires:表示過期時間,默認為天,如上表示過期時間為7天,如果省略過期時間,表示關閉瀏覽器時候,cookie將被刪除

也可以設置cookie的過期時間,如下設置3分鐘后過期

var dt= new date();
dt.settime(date.gettime() + (3* 60 * 1000));
$.cookie('username', 'admin', { expires: dt});

path:定義cookie的有效路徑,默認情況下路徑為創建cookie網頁所在的路徑,如果想讓整個網站訪問到,路徑設置為path: '/'

domain:設置cookie所在的域

secure: 如果為true表示cookie的傳輸需要使用https安全協議,默認為false

2.刪除Cookie

可以使用兩種方法

$.removeCookie("username");
$.cookie('username',"null");

3.讀取cookie

var account = $.cookie('username');

如果沒有寫入cookie,讀取到的值為undefined

附:jquery.cookie.js完整代碼如下:

/*!
 * jQuery Cookie Plugin v1.4.1
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2006, 2014 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {
  if (typeof define === 'function' && define.amd) {
    // AMD (Register as an anonymous module)
    define(['jquery'], factory);
  } else if (typeof exports === 'object') {
    // Node/CommonJS
    module.exports = factory(require('jquery'));
  } else {
    // Browser globals
    factory(jQuery);
  }
}(function ($) {
  var pluses = /+/g;
  function encode(s) {
    return config.raw ? s : encodeURIComponent(s);
  }
  function decode(s) {
    return config.raw ? s : decodeURIComponent(s);
  }
  function stringifyCookieValue(value) {
    return encode(config.json ? JSON.stringify(value) : String(value));
  }
  function parseCookieValue(s) {
    if (s.indexOf('"') === 0) {
      // This is a quoted cookie as according to RFC2068, unescape...
      s = s.slice(1, -1).replace(/\"/g, '"').replace(/\\/g, '\');
    }
    try {
      // Replace server-side written pluses with spaces.
      // If we can't decode the cookie, ignore it, it's unusable.
      // If we can't parse the cookie, ignore it, it's unusable.
      s = decodeURIComponent(s.replace(pluses, ' '));
      return config.json ? JSON.parse(s) : s;
    } catch(e) {}
  }
  function read(s, converter) {
    var value = config.raw ? s : parseCookieValue(s);
    return $.isFunction(converter) ? converter(value) : value;
  }
  var config = $.cookie = function (key, value, options) {
    // Write
    if (arguments.length > 1 && !$.isFunction(value)) {
      options = $.extend({}, config.defaults, options);
      if (typeof options.expires === 'number') {
        var days = options.expires, t = options.expires = new Date();
        t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
      }
      return (document.cookie = [
        encode(key), '=', stringifyCookieValue(value),
        options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
        options.path ? '; path=' + options.path : '',
        options.domain ? '; domain=' + options.domain : '',
        options.secure ? '; secure' : ''
      ].join(''));
    }
    // Read
    var result = key ? undefined : {},
      // To prevent the for loop in the first place assign an empty array
      // in case there are no cookies at all. Also prevents odd result when
      // calling $.cookie().
      cookies = document.cookie ? document.cookie.split('; ') : [],
      i = 0,
      l = cookies.length;
    for (; i < l; i++) {
      var parts = cookies[i].split('='),
        name = decode(parts.shift()),
        cookie = parts.join('=');
      if (key === name) {
        // If second argument (value) is a function it's a converter...
        result = read(cookie, value);
        break;
      }
      // Prevent storing a cookie that we couldn't decode.
      if (!key && (cookie = read(cookie)) !== undefined) {
        result[name] = cookie;
      }
    }
    return result;
  };
  config.defaults = {};
  $.removeCookie = function (key, options) {
    // Must not alter options, thus extending a fresh object...
    $.cookie(key, '', $.extend({}, options, { expires: -1 }));
    return !$.cookie(key);
  };
}));

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



主站蜘蛛池模板: 寄诚庸| 开国大典ppt课件| 黄飞鸿电影全集| 闪电小子| 天与地越南版| 不纽扣的女孩| 国产精品欧美大片| 南来北往分集剧情| 血色残阳剧情简介| 瓯江潮涨潮落时间表| www.黄视频| 李柯以写真照片| 我的冠军男友在线看全集完整| 神经刀| 长句变短句的例题| 纳西三部曲| 碑文格式范例 墓碑图片| 好像也没那么热血沸腾电影免费观看| 周柯宇个人资料| 张宗麟| 神雕侠侣电影| 抖音在线版| 谈判专家豆瓣| 西班牙女郎| 影音先锋欧美| 吉泽明步番号| 最危险的游戏| 性欧洲| 蚀电影| 儿子娶妈妈剧情介绍| 雪暴 电影| 乔什布洛林| 神探狄仁杰1电视剧| 庞敏| 三峡游轮重庆到宜昌4天三夜| free hd xxxx moms movie777| 电影频道直播| 少女模特电影| 卢靖姗个人资料| 王琳琳个人资料及简历| 二年级上册期末真题卷|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103