Wordpress收藏夹插件 - 阅读Cookie

时间:2017-09-06 17:18:11

标签: javascript jquery wordpress cookies favorites

我需要阅读“Wordpress收藏夹”插件生成的cookie(优秀的代码基础),并从cookie数组中获取帖子ID,以便在界面中使用。这是为了避免我们的托管服务提供商WP Engine的前端缓存。

当在Firebug控制台中查看时,cookie被称为“simplefavorites”,cookie数据如下所示:

在“价值”下:

[{"site_id":1,"posts":[17411,22578],"groups":[{"group_id":1,"site_id":1,"group_name":"Default List","posts":[17411,22578]}]}]

并在“原始数据”下:

%5B%7B%22site_id%22%3A1%2C%22posts%22%3A%5B17411%2C22578%5D%2C%22groups%22%3A%5B%7B%22group_id%22%3A1%2C%22site_id%22%3A1%2C%22group_name%22%3A%22Default+List%22%2C%22posts%22%3A%5B17411%2C22578%5D%7D%5D%7D%5D

我需要将post值作为数组来处理类似数组(17411,22578)。我们可以返回整个cookie内容,并检测它是否存在,但只是将ID扯出来证明是棘手的。任何有关如何解析此字符串的建议都将非常感激。这就是我们现在正在使用的......

    // Read Cookie For Favorites Functionality - simplefavourites
    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }
    // Extract post ID's from cookie string
    function getvaluefromcookiestring(cookiecontents){
        cookiecontents = decodeURI(cookiecontents); // this works fine and
        var val = JSON.parse(cookiecontents),
        v = val[0]; // the contents of the cookie is a one-node array
        return v.posts;
    }
    var cookiecontents = readCookie('simplefavorites');
    cookiecontents = getvaluefromcookiestring(cookiecontents);
    alert(cookiecontents);

我们收到一个“SyntaxError:JSON.parse:expected':'在JSON数据的第1行第12列的对象中的属性名后”

------问题解决------ 在@Spartacus和@Louys Patrice Bessette的帮助下。有关正确检索Cookie中的值的信息,请参阅此处的相关问题: Javascript - retrieving data from a cookie as "value" not "raw data"

    // Read cookie for favorites functionality - simplefavourites
    function readCookie(name) {
        var c = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
        return c ? c[2] : null;
    }
    // Extract post ID's from cookie string
    function getvaluefromcookiestring(cookiecontents){
        cookiecontents = decodeURI(cookiecontents.replace(/%3A/g, ":").replace(/%2C/g, ",").replace(/\+/g, "%20"));
        var val = JSON.parse(cookiecontents),
        v = val[0];
        return v.posts;
    }
    var cookiecontents = readCookie('simplefavorites');
    cookiecontents = getvaluefromcookiestring(cookiecontents);
    alert(cookiecontents);

感谢所有帮助堆叠器!

2 个答案:

答案 0 :(得分:1)

它似乎是有效的JSON。使用JSON.parse()。

var cookieString = '[{"site_id":1,"posts":[17411,22578],"groups":[{"group_id":1,"site_id":1,"group_name":"Default List","posts":[17411,22578]}]}]';
var obj = JSON.parse(cookieString);
// now access object properties

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

答案 1 :(得分:1)

将您的cookie-getter功能更改为:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="event" id="btn" type="button">btn</button>
<input class="event" id="input" type="text" name="myInput" value="txt" />

然后使用JSON.parse将值转换为操作对象。你可以像这样抓住它:

function readCookie(name) {
    var c = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
    return c ? c[2] : null;
}