JavaScript Cookie 检查/设置/删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8464502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript Cookies check/set/delete
提问by Jonas
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
我只想检查 cookie 是否存在,设置/重置 cookie 1 天,并在窗口/选项卡关闭时删除 cookie
I'm trying to do this in JavaScript, but hell JS is one dumbass language(I swear), add to that I'm stupid.
我试图在 JavaScript 中做到这一点,但地狱 JS 是一种愚蠢的语言(我发誓),再加上我很愚蠢。
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.
任何类型的答案都值得赞赏并提前谢谢。
采纳答案by FluffyKitten
You can test the Quirksmode code here: http://jsfiddle.net/67RFW/
你可以在这里测试 Quirksmode 代码:http: //jsfiddle.net/67RFW/
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
代码正在运行,所以它一定是其他原因导致了你的问题......所以如果你在 jsfiddle.net 中运行它,看看它对你有什么作用 - 这可能有助于确定它是否是浏览器设置
回答by wsanville
Quirksmode.org has an excellent articleon writing wrapper methods for interacting with document.cookie
(an inherently unfriendly object).
Quirksmode.org 有一篇关于编写用于交互的包装方法的优秀文章document.cookie
(一个本质上不友好的对象)。
The article explains how to implement the following methods: readCookie
, createCookie
, and eraseCookie
.
本文介绍了如何实现以下方法:readCookie
,createCookie
,和eraseCookie
。
From these methods, it's easy to implement your checkCookie
function, that is, if readCookie
returns null or not.
从这些方法中,很容易实现您的checkCookie
函数,即 if 是否readCookie
返回 null。
Here's your checkCookie
:
这是你的checkCookie
:
function checkCookie(name)
{
return readCookie(name) != null;
}
Here's the other three functions that the article provides:
以下是本文提供的其他三个功能:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
回答by Ahmet Can Güven
Here, try it.
来,试试。
function checkcookie(name)
{
if (name!=null && name!="")
{
//reset cookie
cookie_value="test";
document.cookie=name + "=" + cookie_value;
}
else
{
//if there is no cookie, create a new one
cookie_value="test";
document.cookie=name + "=" + cookie_value;
}
}
function setcookie(name,day)
{
var expireDate = new Date();
expireDate.setSeconds((expireDate.getSeconds()+day*24*60*60*1000);
document.cookie = "Name=" + name + ";path=/;expires=" + expireDate.toGMTString();
}
function delcookie(name)
{
setcookie(name,"",-1);
}
回答by FluffyKitten
+1 to wsanville's answer (I don't have enough reputation yet to do so)
+1 wsanville 的回答(我还没有足够的声誉这样做)
I have used the Quirksmode code that he posted and it worked perfectly for me.
我使用了他发布的 Quirksmode 代码,它非常适合我。
Following on from your comment above where you are just getting null, I've tested your code with the code wsanville posted and it works perfectly. If you save the code below as a separate file, does it still not work?
根据您上面的评论,您刚刚获得空值,我已经使用 wsanville 发布的代码测试了您的代码,并且它运行良好。如果将下面的代码另存为单独的文件,它仍然不起作用吗?
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
function checkCookie(name)
{
return readCookie(name) != null;
}
if(checkCookie("XYZ")) {
createCookie("XYZ","SMURF",1);
document.write("reset cookie");
}
else {
createCookie("XYZ","SMURF",1);
document.write(readCookie("XYZ"));
}
</script>
</head>
<body>
</body>
</html>