Extend the document.cookie set function (add condition)
0
I'm trying to extend the document.cookie function, because I want to allow setting cookies only for cookies with specified names. Here is my approach:
Object.defineProperty(document, 'cookie', {
set: function(newValue) {
var acceptedCookieNames = ['cookieselection', 'cookieNoticeAnswered'];
if (
!acceptedCookieNames.some(function(cookieName) {
return newValue.startsWith([cookieName || '', '='].join(''));
})
) {
return false;
}
this._value = newValue;
return this._value;
},
});
No cookie will be set. Can someone help here?
javascript cookies setter
share | improve t...