var ShopCookie = {}
ShopCookie.addc = function createCookie(name,value,days=30) {
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=/";
}
ShopCookie.addc('cookie1','Mytext');
我有这段代码,它在 FireFox 和 Chrome 中运行得很好。但 Opera、EDGE 和 IE 什么都不做!
最佳答案
函数参数声明中的语法 days=30
是新的 ES6 功能,尚未在所有地方得到支持。
您可以回退到旧的默认参数样式:
var ShopCookie = {}
ShopCookie.addc = function createCookie(name,value,days) {
// if days argument not passed, then default it to 30 days
if (arguments.length < 3) {
days = 30;
}
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=/";
}
ShopCookie.addc('cookie1','Mytext');
仅供引用,您应该在调试控制台中看到错误,这些错误准确地告诉您浏览器不喜欢哪种语法。每当您有无法运行的代码时,您总是会在那里查找。
关于javascript - 我无法在 Opera 和 Internet Explorer 中设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36927762/