JavaScript Cookies检查/设置/删除 [英] JavaScript Cookies check/set/delete
问题描述
我只想检查cookie是否存在,
设置/重置cookie 1天,
,并在关闭窗口/选项卡时删除cookie
I just wanna check if a cookie exists, set/reset the cookie for 1 day, and delete the cookie when the window/tab is closed
我正在尝试使用JavaScript进行此操作,但是地狱JS是一种愚蠢的语言(我发誓),再加上我很愚蠢。
I'm trying to do this in JavaScript, but hell JS is one dumbass language(I swear), add to that I'm stupid.
无论如何,这就是我在做什么:
Anyways here's what i'm doing:
function checkcookie(name)
{
//Can't figure this one out
//I want to check wheather the cookie is set or not
//if yes
//{ reset cookie + ... }
//else
//{ set cookie + ... }
}
function setcookie(name,value,day)
{
var expireDate = new Date();
expireDate.setSeconds(expireDate.getSeconds()+day*24*60*60*1000);
document.cookie = name + "=" + value + ";path=/;expires=" + expireDate.toGMTString();
}
function delcookie(name)
{
setcookie(name,"",-1);
}
任何一种答案都应得到赞赏,并提前提出。
Any kind of answer is appreciated and thx in advance.
推荐答案
您可以在此处测试Quirksmode代码:
http://jsfiddle.net/67RFW/
You can test the Quirksmode code here: http://jsfiddle.net/67RFW/
该代码正在运行,因此可能是其他原因导致您的问题...所以如果您在jsfiddle.net中运行它,将会看到它的作用-这可能有助于确定它是否是浏览器设置
The code is working so it must be something else causing your problem... so see what it does for you if you run it in jsfiddle.net - that might help determine if its a browser setting
这篇关于JavaScript Cookies检查/设置/删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!