create a cookie with javascript in greasemonkey

試著忘記壹切 提交于 2019-12-10 14:22:30

问题


i'm trying to create a cookie with greasemonkey in order to stop a window from popping up (after the windows pops up a cookie is created the the window won't popup to 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 no cookie is been created....


回答1:


If you set a cookie that has an expires value equal to, or older than, the current system clock, it actually deletes the named cookie instead (Unless the path or domain are different, or it is a "secure" cookie -- none of which apply here).

This:

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

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

To actually set a new cookie, use:

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

which will cause your code to set a session cookie -- which is probably what you want.

Or use:

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

To set a cookie that expires in a day.



来源:https://stackoverflow.com/questions/13452626/create-a-cookie-with-javascript-in-greasemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!