在greasemonkey中使用javascript创建一个cookie [英] create a cookie with javascript in greasemonkey
问题描述
我试图创建一个cookie与greasemonkey为了阻止一个窗口弹出(在窗口弹出一个cookie后创建的窗口不会弹出很多次...
这个是代码
函数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);
}
但没有创建cookie ....
解决方案如果您设置的Cookie的
expires
值等于或早于当前系统时钟,则实际上 (除非路径
或网域
不同,
这:
code> setCookie(cookie_names [i],1,0);
导致该函数设置具有即时到期值的Cookie,从而有效删除具有该名称的任何Cookie。
要实际设置新的Cookie,请使用:
setCookie cookie_names [i],1,null);
这将导致您的代码设置会话cookie - 这可能是你想要的。 p>
或使用:
setCookie(cookie_names [i],1,1 );
设置一天过期的Cookie。
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....
解决方案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 thepath
ordomain
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.
这篇关于在greasemonkey中使用javascript创建一个cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!