Create javascript cookie in greasemonkey

I am trying to create a cookie with greasemonkey to stop the popup (after the windows pop up, the cookie is created, the window will not pop up many times ... this is the code

function setCookie(c_name, value, expiredays) { var exdate = new Date(); exdate.setDate(exdate.getDate()+expiredays); document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toUTCString()); } var cookie_names = [ 'showDrushimPopUnderUserClick', 'showDrushimPopUnder308' ]; for (var i in cookie_names) { setCookie(cookie_names[i], 1, 0); } 

but cookie not created ...

+4
source share
1 answer

If you set a cookie with an expires value equal to or older than the current system clock, it actually deletes the named cookie (if path or domain different, or if it is a โ€œsafeโ€ cookie - none of them apply here).

It:

 setCookie(cookie_names[i], 1, 0); 

Forces this function to set a cookie with an instant expiration value, effectively deleting any cookie with this name.

To set a new cookie, use:

 setCookie(cookie_names[i], 1, null); 

which will force your code to set a session cookie - which is probably what you need.

Or use:

 setCookie(cookie_names[i], 1, 1); 

To set a cookie that expires in a day.

+4
source

Source: https://habr.com/ru/post/1446929/


All Articles