前端cookie

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   使用   sp   

cookie 封裝js

/*** Cookie plugin** Copyright (c) 2006 ziqiu.zhang * Dual licensed under the MIT and GPL licenses:* http://www.opensource.org/licenses/mit-license.php* http://www.gnu.org/licenses/gpl.html* 使用舉例:    //註: 寫入時,subName參數請傳遞空值或null    //寫入Cookies-值為字串,即不包含子鍵    $.cookie("singleKey", "", "singleKey-value", { expires: 1, path: "/", secure: false })    //讀取Cookies-根據主鍵    alert("singleKey:" + $.cookie("singleKey"));    //寫入Cookies-值為對象,則每個屬性名稱為子鍵的名稱,屬性值為子索引值    var subNameObj = { subName1: "aaa", subName2: "bbb", subName3: "ccc" };    $.cookie("multiKey", "", subNameObj, { expires: 1, path: "/", secure: false });    //讀取Cookies-根據主鍵    alert("multiKey:" + $.cookie("multiKey"));    //讀取Cookies-根據主鍵和子鍵    alert("multiKey,subName1:" + $.cookie("multiKey", "subName1"));**/jQuery.cookie = function (name, subName, 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        }        // CAUTION: 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) : ‘;path=/‘;        var domain = options.domain ? ‘; domain=‘ + (options.domain) : ‘‘;        var secure = options.secure ? ‘; secure‘ : ‘‘;        //If value is an object, each property will be a sub key;        if (typeof value == "object") {            var k = 0;            var tempResult = "";            for (var tempValue in value) {                if (k > 0) {                    tempResult += "&";                }                tempResult += tempValue + "=" + encodeURIComponent(value[tempValue]);                k++;            }            value = tempResult;        }        else {            value = encodeURIComponent(value);        }        document.cookie = [name, ‘=‘, 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));                    //Search sub key                    if (typeof subName != ‘undefined‘ && subName != null && subName != "") {                        var subCookies = cookieValue.toString().split(‘&‘);                        for (var j = 0; j < subCookies.length; j++) {                            var subCookie = jQuery.trim(subCookies[j]);                            if (subCookie.substring(0, subName.length + 1) == (subName + ‘=‘)) {                                cookieValue = decodeURIComponent(subCookie.substring(subName.length + 1));                                break;                            }                        }                    }                    break;                }            }        }        return cookieValue;    }};

寫入cookie

  var cityCookieKey = "Citys";        $(function () {            if ($.cookie(cityCookieKey, "values") != null && $.cookie(cityCookieKey, "name") != null) {  //讀取                $("#citys").attr("lang", $.cookie(cityCookieKey, "values"));                $("#citys").html($.cookie(cityCookieKey, "name") + "&nbsp;<img src=\"/Content/User/icon/add_icon.gif\" />");            }            $(".check_city").addClass("hide");            $(".check_city").hide();            $(".check_city a").bind("click", function () {                if ($.cookie(cityCookieKey, "values") != null && $.cookie(cityCookieKey, "name") != null) {                    $.cookie(cityCookieKey, "", "", { expires: 1, path: "/", secure: false }); //寫入                }                $(".check_city").addClass("hide");                $(".check_city").hide();                $(".check_city").removeClass("show");                var citys = $(this).html();                var value = $(this).attr("lang");                var cookieFilter = {                    values: value,                    name: citys                };                $.cookie(cityCookieKey, "", cookieFilter, { expires: 1, path: "/", secure: false });  //寫入                $("#citys").attr("lang", $.cookie(cityCookieKey, "values"));                $("#citys").html($.cookie(cityCookieKey, "name") + "&nbsp;<img src=\"/Content/User/icon/add_icon.gif\" />");            });        });

 

前端cookie

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.