【问题标题】:jQuery Cookie Plugin reading ValuejQuery Cookie 插件读取值
【发布时间】:2014-01-25 04:29:33
【问题描述】:

我想让这个东西工作,但我不知道怎么做。

如果设置了 cookie 并且 cookie 的值匹配,我想显示一个横幅。

实际上我可以读取 cookie 但不能读取值。如果 cookie 存在,则显示横幅 - 这一个完成。但如果 cookie 存在且值匹配,则应显示横幅。

这些是 cookie 信息:

这个应该匹配一个显示横幅:

名称:myCookie 值:a%3A2%3A%7Bs%3A17%3A%22bmbankloginBankId%22%3Bs%3A1%3A%221%22%3Bs%3A27%3A%22bmbankloginDisclaimerSigned%22%3Bb%3A1%3B%7D 主机:www.mydomain.de 小路: / 过期:格林威治标准时间 2043 年 12 月 30 日星期三 23:26:25

具有其他值的相同 cookie,最终不应显示横幅:

名称:myCookie 值:a%3A2%3A%7Bs%3A17%3A%22bmbankloginBankId%22%3Bs%3A2%3A%2229%22%3Bs%3A27%3A%22bmbankloginDisclaimerSigned%22%3Bb%3A1%3B%7D 主机:www.mydomain.de 小路: / 过期:2043 年 12 月 31 日星期四 08:57:36 GMT

这是我的脚本:

<div id="partner" class="position_helper_banner"><a href="http://www.google.de/" target="_blank" title="something"><img src="partner-banner-180x73.png" /></a></div>
<script type="text/javascript">
$(function(){
  if($.cookie('myCookie')) {
    $('#partner').show();
  }
});
</script>

通过 CSS 将 #partner 设置为 display:none。

jQuery 插件包含以下内容:

/*!
 * jQuery Cookie Plugin v1.3.1
 * https://github.com/carhartl/jquery-cookie
 *
 * Copyright 2013 Klaus Hartl
 * Released under the MIT license
 */
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as anonymous module.
        define(['jquery'], factory);
    } else {
        // Browser globals.
        factory(jQuery);
    }
}(function ($) {

    var pluses = /\+/g;

    function decode(s) {
        if (config.raw) {
            return s;
        }
        return decodeURIComponent(s.replace(pluses, ' '));
    }

    function decodeAndParse(s) {
        if (s.indexOf('"') === 0) {
            // This is a quoted cookie as according to RFC2068, unescape...
            s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
        }

        s = decode(s);

        try {
            return config.json ? JSON.parse(s) : s;
        } catch(e) {}
    }

    var config = $.cookie = function (key, value, options) {

        // Write
        if (value !== undefined) {
            options = $.extend({}, config.defaults, options);

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = config.json ? JSON.stringify(value) : String(value);

            return (document.cookie = [
                config.raw ? key : encodeURIComponent(key),
                '=',
                config.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // Read
        var cookies = document.cookie.split('; ');
        var result = key ? undefined : {};
        for (var i = 0, l = cookies.length; i < l; i++) {
            var parts = cookies[i].split('=');
            var name = decode(parts.shift());
            var cookie = parts.join('=');

            if (key && key === name) {
                result = decodeAndParse(cookie);
                break;
            }

            if (!key) {
                result[name] = decodeAndParse(cookie);
            }
        }

        return result;
    };

    config.defaults = {};

    $.removeCookie = function (key, options) {
        if ($.cookie(key) !== undefined) {
            // Must not alter options, thus extending a fresh object...
            $.cookie(key, '', $.extend({}, options, { expires: -1 }));
            return true;
        }
        return false;
    };

}));

那我该怎么办??

【问题讨论】:

    标签: jquery cookies jquery-cookie


    【解决方案1】:

    当页面第一次(或任何时候)加载时设置cookie:

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

    在后续加载时获取它的值(如果未设置,它将是未定义的):

    var cookieValue = $.cookie('the_cookie');
    

    然后决定做什么:

    var requiredValue = "whatever";
    if(cookieValue === requiredValue){
      $('#partner').show();
    }
    

    编辑:

    类似这样的: $.cookie.json = true;

    var cookieValue = {
      value1: "string",
      value2: integer,
      value3: {object}
    }
    
    $.cookie('my_cookie', cookieValue, { expires: 7, path: '/' });
    
    var cookieValue = $.cookie('the_cookie'),
        requiredValue = "whatever";
    
    if(cookieValue[key] === requiredValue){
      $('#partner').show();
    }
    

    【讨论】:

    • 太好了,这行得通!谢谢!但是 cookie 的值是一种数组(我认为),并且只有一个参数是我正在搜索的指标。这是数组:"a:2:{s:17:"bmbankloginBankId";s:1:"1";s:27:"bmbankloginDisclaimerSigned";b:1;}" - 我只是在寻找 "" bmbankloginBankId";s:1:"1";"。我该怎么办?非常感谢!!!!
    • cookie的值是一个字符串(除非你指定$.cookie.json = true)。如果您必须存储您列出的所有信息,请将其粘贴在一个对象中,将其保存起来,并在您检索到它时进行相应的引用。
    • 谢谢,但我不知道该怎么做。请再给我一个提示好吗?
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多