javascript - 为什么我无法在 Google Chrome 中返回 cookie?

标签 javascript html cookies

我正在练习 document.cookie 属性,并且能够在我的 FireFox 浏览器中设置和返回 cookie。但是,当我尝试在 google chrome 中返回一些 cookie 时,我没有返回任何内容。这是有原因的吗?这是我在 jsfiddle 中的代码.

HTML

<div id="ex1">
    <h2>Example 1</h2>
    <p></p>
    <h4>results:</h4>
    <button id="btn">Return Cookie</button>
    <button id="btn2">Set Cookie</button>
</div>

Javascript

function cText(text) {
    return document.createTextNode(text);
}

function cElem(elem) {
    return document.createElement(elem);
}

function attach(id, text, elem) {
    var a = cText(text);
    var b = cElem(elem);
    b.appendChild(a);

    var c = document.getElementById(id).appendChild(b);
    return c;
}

document.getElementById('btn').onclick = function() {
    var a = document.cookie;
    attach('ex1', a, 'p');
}

/* In order to make key = values you have to make a  separate line for 
each name and value that you are going to put into the document.cookie*/
document.getElementById('btn2').onclick = function() {
    document.cookie = "name = Michael Hendricks; expires =" + dayt + ";";
    document.cookie = "occupation = Shitty Retail Clerk; expires =" + dayt + ";";
    document.cookie = "age = 26 years old with big ol man boobs; expires =" + dayt + ";";

    console.log('cookie has been saved');
}

最佳答案

如果您在本地主机上设置 cookie,则域必须为空。

关于javascript - 为什么我无法在 Google Chrome 中返回 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37228489/

相关文章:

javascript - 如何在 JavaScript 中查找多个表单输入值的数字总和?

javascript - 在 Javascript 中将数组传递给 Web 方法

javascript - 简单的 javascript getProperty 函数无法访问 CSS 样式,即使它应该可以?

html - 显示带有内联图像的单选按钮的验证消息

Javascript:使 cookie 在 5 分钟内过期

jsf - IBM AppScan - 加密 session (SSL) Cookie 中缺少安全属性

javascript - 如何从javascript中隐藏字段的值中删除单引号

html - 用户点击同页链接后是否可以突出显示页面的某个区域?

python - 如何手动向已在 mechanize 中设置 cookie 的 session 添加更多 cookie?

javascript - JQuery 设置后找不到 .NET 隐藏字段值