溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jquery.cookie.js的示例分析

發(fā)布時(shí)間:2021-07-27 14:19:44 來(lái)源:億速云 閱讀:182 作者:小新 欄目:web開發(fā)

這篇文章主要為大家展示了“jquery.cookie.js的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“jquery.cookie.js的示例分析”這篇文章吧。

什么是 cookie?

cookie 就是頁(yè)面用來(lái)保存信息,比如自動(dòng)登錄、記住用戶名等等。

cookie 的特點(diǎn)

  1. 同個(gè)網(wǎng)站中所有的頁(yè)面共享一套 cookie

  2. cookie 有數(shù)量、大小限制

  3. cookie 有過期時(shí)間jquery.cookie.js 是一款輕量級(jí)的 cookie 插件,可以讀取,寫入和刪除 cookie。本文主要針對(duì)

jquery.cookie.js 的用法進(jìn)行詳細(xì)的介紹。

jquery.cookie.js 使用方法

Cookies

定義:讓網(wǎng)站服務(wù)器把少量數(shù)據(jù)儲(chǔ)存到客戶端的硬盤或內(nèi)存,從客戶端的硬盤讀取數(shù)據(jù)的一種技術(shù);

下載與引入:jquery.cookie.js基于jquery;先引入jquery,再引入:jquery.cookie.js;

下載:http://plugins.jquery.com/cookie/

<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>

使用:

1.添加一個(gè)"會(huì)話cookie"

$.cookie('the_cookie', 'the_value');

這里沒有指明 cookie有效時(shí)間,所創(chuàng)建的cookie有效期默認(rèn)到用戶關(guān)閉瀏覽器為止,所以被稱為 “會(huì)話cookie(session cookie)”。

2.創(chuàng)建一個(gè)cookie并設(shè)置有效時(shí)間為 7天

$.cookie('the_cookie', 'the_value', { expires: 7 });

這里指明了cookie有效時(shí)間,所創(chuàng)建的cookie被稱為“持久 cookie (persistent cookie)”。注意單位是:天;

3.創(chuàng)建一個(gè)cookie并設(shè)置 cookie的有效路徑

$.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });

在默認(rèn)情況下,只有設(shè)置 cookie的網(wǎng)頁(yè)才能讀取該 cookie。如果想讓一個(gè)頁(yè)面讀取另一個(gè)頁(yè)面設(shè)置的cookie,必須設(shè)置cookie的路徑。cookie的路徑用于設(shè)置能夠讀取 cookie的頂級(jí)目錄。將這個(gè)路徑設(shè)置為網(wǎng)站的根目錄,可以讓所有網(wǎng)頁(yè)都能互相讀取 cookie (一般不要這樣設(shè)置,防止出現(xiàn)沖突)。

4.讀取cookie

$.cookie('the_cookie');

5.刪除cookie

$.cookie('the_cookie', null);   //通過傳遞null作為cookie的值即可

6.可選參數(shù)

$.cookie('the_cookie','the_value',{
  expires:7, 
  path:'/',
  domain:'jquery.com',
  secure:true
}) 

expires:(Number|Date)有效期;設(shè)置一個(gè)整數(shù)時(shí),單位是天;也可以設(shè)置一個(gè)日期對(duì)象作為Cookie的過期日期;
path:(String)創(chuàng)建該Cookie的頁(yè)面路徑;
domain:(String)創(chuàng)建該Cookie的頁(yè)面域名;
secure:(Booblean)如果設(shè)為true,那么此Cookie的傳輸會(huì)要求一個(gè)安全協(xié)議,例如:HTTPS;

使用方法:

設(shè)置 cookie:

$.cookie('the_cookie', 'the_value');

注:如果 $.cookie 沒有第三個(gè)參數(shù),那么當(dāng)瀏覽器關(guān)閉時(shí),該 cookie 將會(huì)自動(dòng)刪除。

設(shè)置一個(gè)有效期為 7 天的 cookie:

$.cookie('the_cookie', 'the_value', {expires: 7});

注:$.cookie 第三個(gè)參數(shù)是一個(gè)對(duì)象,除了可以設(shè)置有效期(expires: 7),還可以設(shè)置有效路徑(path: '/')、有效域(domain: 'jquery.com')及安全性(secure: true)。

讀取 cookie:

$.cookie('the_cookie');

注:如果沒有該 cookie,返回 null。

刪除 cookie:

$.cookie('the_cookie', null);

我們只需要給需要?jiǎng)h除的 cookie 設(shè)置為 null,就可以刪除該 cookie。

最后附上源代碼:

/**
 * 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
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 * used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *    If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *    If set to null or omitted, the cookie will be a session cookie and will not be retained
 *    when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *   require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * 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 = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
  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
 }
 // NOTE Needed to parenthesize options.path and options.domain
 // in the following expressions, otherwise they evaluate to undefined
 // in the packed version for some reason...
 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;
 }
};

以上是“jquery.cookie.js的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI