首頁  >  文章  >  web前端  >  可相容IE的取得及設定cookie的jquery.cookie函數方法_jquery

可相容IE的取得及設定cookie的jquery.cookie函數方法_jquery

WBOY
WBOY原創
2016-05-16 17:23:541462瀏覽
前言

在開發過程中,因為之前有接觸過Discuz,就直接拿其common.js裡面的getcookie和setcookie方法來使用,做到後面在使用IE來測試的時候,發現這兩個方法子啊IE下不起作用,就請教同事,這樣就有了jquery.cookie.js檔案的由來,裡面的程式碼很少,我貼在下面,方便以後使用和研究吧。

原始碼
複製程式碼 程式碼>/**
* Cookie 外掛程式
*
* 版權所有(c) 2006 Klaus Hartl (stilbuero.de)
* 根據MIT 與GPL 授權雙重授權:
* http://www. opensource .org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

/**
* 使用給定的名稱和值以及其他可選參數建立 cookie。
*
* @example $.cookie('the_cookie', 'the_value');
* @desc 設定 cookie 的值。
* @example $.cookie('the_cookie', 'the_value', {過期: 7, 路徑: '/', 網域: 'jquery.com', secure: true});
* @desc 建立一個包含所有可用選項的cookie。
* @example $.cookie('the_cookie', 'the_value');
* @desc 建立會話 cookie。
* @example $.cookie('the_cookie', null);
* @desc 透過傳遞 null 作為值來刪除 cookie。
*
* @param String name cookie 的名稱。
* @param String value cookie 的值。
* @param 物件選項 包含鍵/值對的物件文字,以提供可選的 cookie 屬性。
* @option Number|Date expires 指定從現在開始的到期日(以天為單位)的整數或 Date 物件。
* 如果指定負值(例如過去的日期),cookie 將被刪除。
* 如果設定為 null 或省略,則 cookie 將是會話 cookie,並且在瀏覽器退出時不會保留
*。
* @option String path cookie 的路徑屬性值(預設:建立 cookie 的頁面路徑)。
* @option String domain cookie 的網域屬性值(預設:建立 cookie 的頁面的網域)。
* @option Boolean secure 如果為 true,將設定 cookie 的安全屬性,且 cookie 傳輸將
* 需要安全協定(如 HTTPS)。
* @type undefined
*
* @name $.cookie
* @cat 外掛程式/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* 取得具有給定名稱的 cookie 的值。
*
* @example $.cookie('the_cookie');
* @desc 取得 cookie 的值。
*
* @param String name cookie 的名稱。
* @return cookie 的值。
* @type String
*
* @name $.cookie
* @cat 外掛程式/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='ate. (); // use expires attribute, max-age is not supported by IE
}
var path = options.path ? '; path=' options.path : '';
var path .domain ? '; domain=' options.domain : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIonent(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 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;
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn