javascript 在子域/读取cookie之间设置javascript cookie

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 在子域/读取cookie之间设置javascript cookie相关的知识,希望对你有一定的参考价值。

function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) === 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
// Data to store
var LastReportGenerated="Jul 11 2013",
// Get the top level domain and assign as .domain.com
baseDomain = '.cssjunction.com', //your domain must be in format of '.domain.com' – dot and root domain
// Set Expires (optional)
expireAfter = new Date();
expireAfter.setDate(expireAfter.getDate() + 7); //setting up  cookie expire date after a week

// Now setup cookie
document.cookie="Report={'ReportName':'MainReport', 'lastGenerated':" + LastReportGenerated + "}; domain=" + baseDomain + "; expires=" + expireAfter + "; path=/"; // 'path=/'' always. If you don’t setup your path=/, auto path will be saved as from where the cookies is being saved hence it wont be accessible across any subdomain.

以上是关于javascript 在子域/读取cookie之间设置javascript cookie的主要内容,如果未能解决你的问题,请参考以下文章

是否可以在子域之间共享 cookie

如果所有子域都指向同一个网站,cookie 是不是会在多个子域之间共享? [复制]

在 Rails 中的子域之间共享会话(cookie)?

Chrome 未在子域之间共享 cookie

使用 IIS express 在域和子域之间共享 cookie

是否可以删除子域 cookie?