如何获取文件.饼干工作正常

How to get document.cookie to work properly?

本文关键字:饼干 工作 文件 何获取 获取      更新时间:2023-09-26

我一直在尝试设置一个简单的cookie到当前页面,但它似乎不起作用(它不显示我有一个cookie保存和任何警告与文档。Cookie显示没有文本

Cookies = {};
Cookies.cookiefile = blue;
Cookies.cookiekey = 5;
function Cookies.save(){
document.cookie= Cookies.cookiefile+"="+Cookies.cookiekey+";max-age="+60*60*24*10+";path=/";
alert(document.cookie);
}

现在您已经显示了您的实际代码,似乎您有一个脚本错误,使您的代码无法运行。这段代码:

function Cookies.save(){...} 

不是声明函数的正确语法。您应该检查浏览器错误控制台或调试控制台以查看脚本错误,它可能会向您显示这些错误。你可以使用以下格式:

Cookies.save = function() {...}

在OP提供任何代码之前提供的这部分答案。

这是一组处理cookie的实用函数。没有看到你的代码,我们无法真正知道你做错了什么,但如果你使用这些函数并尝试从允许的页面访问cookie,它应该工作。

// createCookie()
// name and value are strings
// days is the number of days until cookie expiration
// path is optional and should start with a leading "/" 
//   and can limit which pages on your site can 
//   read the cookie.
//   By default, all pages on the site can read
//   the cookie if path is not specified
function createCookie(name, value, days, path) {
    var date, expires = "";
    path = path || "/";
    if (days) {
        date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=" + path;
}
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;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}