如何让 Javascript cookie 存储在本地主机上?
How to get Javascript cookie to store on localhost?
我正在尝试使用 javascript 从 html 文件创建和存储 cookie 数据。第一个函数创建一个 cookie,第二个函数用于存储它。
调用该函数时,控制台正确记录了所需的信息,但似乎没有创建 cookie。
JS:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + "; " + expires + "; path=/";
console.log('Cookie created as ' + name + "=" + value + ". Expiration: " + expires);
}
function readCookie(name) {
var x = retrieveCookie(name);
if (x) {
window.alert(x);
console.log('Cookie passed to alert menu')
}
else {
console.log('Cookie was undefined and did not return');
}
}
像上面提到的 Vitalii,它确实需要 运行 来自服务器。我 运行 从 Windows 10 IIS 中挖掘,它与上面发布的代码配合得很好。
我正在尝试使用 javascript 从 html 文件创建和存储 cookie 数据。第一个函数创建一个 cookie,第二个函数用于存储它。
调用该函数时,控制台正确记录了所需的信息,但似乎没有创建 cookie。
JS:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name + "=" + value + "; " + expires + "; path=/";
console.log('Cookie created as ' + name + "=" + value + ". Expiration: " + expires);
}
function readCookie(name) {
var x = retrieveCookie(name);
if (x) {
window.alert(x);
console.log('Cookie passed to alert menu')
}
else {
console.log('Cookie was undefined and did not return');
}
}
像上面提到的 Vitalii,它确实需要 运行 来自服务器。我 运行 从 Windows 10 IIS 中挖掘,它与上面发布的代码配合得很好。