Reputation: 63
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');
I have this code and it works great in FireFox and Chrome. But opera, EDGE and IE does nothing!
Upvotes: 1
Views: 104
Reputation: 707288
The syntax days=30
in the function argument declaration is new ES6 functionality that is not yet supported everywhere.
You can fallback to the older style of defaulting arguments:
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');
FYI, you should be seeing errors in the debug console that tell you exactly what syntax the browser does not like. You would always be looking there whenever you have code that isn't working.
Upvotes: 1