国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 擴展 > jQuery > 正文

jQuery操作Cookie的插件使用方法

2024-09-06 20:04:36
字體:
來源:轉載
供稿:網友

代碼:

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的插件,大概的使用方法如下
$.cookie(
'the_cookie'); //讀取Cookie值
$.cookie('the_cookie', 'the_value'); //設置cookie的值
$.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});//新建一個cookie 包括有效期 路徑 域名等
$.cookie('the_cookie', 'the_value'); //新建cookie
$.cookie('the_cookie', null); //刪除一個cookie


設置一個名稱為blog,值為css9.net的cookie:

$.cookie("blog", "css9.net");

設置一個名稱為blog,值為css9.net的cookie,同時設置過期時間(expires屬性)為7天:

$.cookie("blog", "css9.net", { expires: 7 });

設置一個名稱為blog,值為css9.net的cookie,設置過期時間(expires屬性)為7天,同時設置cookie的path屬性為”/admin”

$.cookie("blog", "css9.net", { path: '/admin', expires: 7 });

讀取Cookie:

讀取名稱為blog的cookie值:

alert( $.cookie("blog") );

刪除cookie:

$.cookie("example", null);

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松原市| 麦盖提县| 银川市| 莱西市| 弋阳县| 长岛县| 孟州市| 商丘市| 双江| 筠连县| 杨浦区| 凤山市| 修武县| 青神县| 合水县| 融水| 陇西县| 利川市| 鸡东县| 阿克| 商河县| 新源县| 邵阳县| 凉城县| 蓝山县| 库尔勒市| 新沂市| 资阳市| 德令哈市| 南川市| 铁岭市| 闸北区| 白玉县| 班戈县| 黔南| 桃源县| 淳化县| 辽宁省| 梅州市| 中西区| 嘉黎县|