Jquery很好吃的Cookie

Jquery very tasty Cookie

本文关键字:Cookie 很好 Jquery      更新时间:2023-09-26

我有代码设置Cookie为每个文件夹的域(例如,domain.com和domain.com/folder, domain.com/folder2)有相同的Cookie名称("历史")和不同的"历史"值。我怎么能忽略路径Cookie和添加所有Cookie值在同一名称("历史"),即使Cookie是从另一个文件夹设置?我使用了标准的cookie.js (https://github.com/js-cookie/js-cookie)和以下附加代码:

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + '=' + cvalue + ';path="/";' + expires;
}
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for ( var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0)
            return c.substring(name.length, c.length);
    }
    return "";
}
function checkHistory(targetId) {
    var history = getCookie("history");
    var htmlContent = '';
    if (history != "") {
        var insert = true;
        var sp = history.toString().split(",");
        for ( var i = sp.length - 1; i >= 0; i--) {
            htmlContent += '<div id="recentViewes" data-recentViewes="'
                    + sp[i] + '"></div> ';
            if (sp[i] == document.URL) {
                insert = false;
            }
            document.getElementById(targetId).innerHTML = htmlContent;
        }
        if (insert) {
            sp.push(document.URL);
        }
        setCookie("history", sp.toString(), 30);
    } else {
        var stack = new Array();
        stack.push(document.URL);
        setCookie("history", stack.toString(), 30);
    }
}

自己找到解决办法:

这段代码用于你想要拥有历史块的页面:

function checkHistory(targetId) {
    var history = getCookie("history");
    var htmlContent = '';
    if (history != "") {
        var insert = true;
        var sp = history.toString().split(",");
        var se = decodeURIComponent(sp).split(",");
        for ( var i = se.length - 1; i >= 0; i--) {
            htmlContent += '<div id="recentViewes" data-recentViewes="'
                    + se[i] + '"></div> ';
            if (se[i] == window.location.pathname) {
                insert = false;
            }
            document.getElementById(targetId).innerHTML = htmlContent;
        }
        if (insert) {
            se.push(window.location.pathname);
        }
        setCookie("history", se.toString(), 30);
    } else {
        var stack = new Array();
        stack.push(window.location.pathname);
        setCookie("history", stack.toString(), 30);
    }
}

对于每个文件夹/页面的域名:

function checkHistory(targetId) {
    var history = getCookie("history");
    var htmlContent = '';
    if (history != "") {
        var insert = true;
        var sp = history.toString().split(",");     
        var se = decodeURIComponent(sp).split(",");
        for ( var i = se.length - 1; i >= 0; i--) {
            htmlContent += '<div id="recentViewes" data-recentViewes="'
                    + se[i] + '"></div> ';
            if (se[i] == window.location.pathname) {
                insert = false;
            }
            document.getElementById(targetId).innerHTML = htmlContent;
        }
        if (insert) {
            se.push(window.location.pathname);
        }
 $.cookie('history', se.toString(), { expires: 30, path: '/' });
    } else {
        var stack = new Array();
        stack.push(window.location.pathname);
        $.cookie('history', stack.toString(), { expires: 30, path: '/' });
    }
}