我正在练习 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/