可兼容IE的獲取及設(shè)置cookie的jquery.cookie函數(shù)方法
在開(kāi)發(fā)過(guò)程中,因?yàn)橹坝薪佑|過(guò)Discuz,就直接拿其common.js里面的getcookie和setcookie方法來(lái)使用,做到后面在使用IE來(lái)測(cè)試的時(shí)候,發(fā)現(xiàn)這兩個(gè)方法子啊IE下不起作用,就請(qǐng)教同事,這樣就有了jquery.cookie.js文件的由來(lái),里面的代碼很少,我貼在下面,方便以后使用和研究吧。
源碼
/**
* 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.
*
* @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.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;
}
};
- jQuery通過(guò)寫(xiě)入cookie實(shí)現(xiàn)更換網(wǎng)頁(yè)背景的方法
- Jquery操作cookie記住用戶名
- jquery中cookie用法實(shí)例詳解(獲取,存儲(chǔ),刪除等)
- jquery.cookie.js用法實(shí)例詳解
- jquery.cookie實(shí)現(xiàn)的客戶端購(gòu)物車操作實(shí)例
- jQuery基于cookie實(shí)現(xiàn)的購(gòu)物車實(shí)例分析
- jQuery操作cookie方法實(shí)例教程
- jQuery的cookie插件實(shí)現(xiàn)保存用戶登陸信息
- jquery.cookie() 方法的使用(讀取、寫(xiě)入、刪除)
- jquery 刪除cookie失效的解決方法
- 詳解jQuery的Cookie插件
相關(guān)文章
JQuery動(dòng)態(tài)創(chuàng)建DOM、表單元素的實(shí)現(xiàn)代碼
代碼很簡(jiǎn)單,直接上代碼。別忘記引用JQuery包。2011-08-08jQuery插件uploadify實(shí)現(xiàn)ajax效果的圖片上傳
本文主要介紹使用jQuery插件uploadify實(shí)現(xiàn)ajax效果的圖片上傳的功能,有需要的朋友可以參考一下。2016-06-06jQuery的中 is('':visible'') 解析及用法(必看)
下面小編就為大家?guī)?lái)一篇jQuery的中 is(':visible') 解析及用法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02jQuery實(shí)現(xiàn)列表自動(dòng)滾動(dòng)循環(huán)滾動(dòng)展示新聞
jQuery實(shí)現(xiàn)列表自動(dòng)滾動(dòng)循環(huán)滾動(dòng)展示新聞,鼠標(biāo)懸停時(shí)停止?jié)L動(dòng)并提示,離開(kāi)后,繼續(xù)滾動(dòng),實(shí)現(xiàn)的代碼如下,需要的朋友可以看看2014-08-08jQuery插件HighCharts繪制的2D堆柱狀圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制的2D堆柱狀圖效果,結(jié)合完整實(shí)例形式分析了jQuery插件HighCharts繪制2D柱狀圖的實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03jQuery+css實(shí)現(xiàn)非常漂亮的水平導(dǎo)航菜單效果
這篇文章主要介紹了jQuery+css實(shí)現(xiàn)非常漂亮的水平導(dǎo)航菜單效果,通過(guò)each方法遍歷元素再使用removeClass與addClass變換頁(yè)面元素樣式實(shí)現(xiàn)導(dǎo)航菜單的切換效果,需要的朋友可以參考下2016-07-07jQuery中g(shù)et和post方法傳值測(cè)試及注意事項(xiàng)
jQuery 的 get 和 post 方法有三個(gè)參數(shù):地址,數(shù)據(jù) 和回調(diào)函數(shù),剛剛做了幾個(gè)實(shí)驗(yàn),看看下面的代碼就清楚了2014-08-08