简体   繁体   English

简单的Javascript / JQuery Cookie问题

[英]Simple Javascript/JQuery Cookie Question

I've got an animation that I want to play only once and I figured that since the animation was in JQuery, the function to write the cookie file should be as well. 我有一个动画,我只想播放一次,所以我发现由于该动画在JQuery中,因此编写cookie文件的功能也应该是这样。

So far I've got: 到目前为止,我已经:

$(document).ready(function(){

if (!$.cookie('cookieName'))
 {
    setTimeout(function() {    
    $('#intro').fadeOut(1000); 
 }, 5000);
};

$.cookie('cookieName', 'the_value');

});

I guess I was thinking this was going to check to see if there was a cookie and if not then play the animation. 我想我当时在想要检查是否有Cookie,如果没有,则播放动画。 When that was done it writes a cookie and so when I come back to the page it doesn't play. 完成后,它会写一个cookie,因此当我回到页面时,它不会播放。 I'm calling the Klaus Hartl cookie plugin and for some reason this just isn't doing it for me. 我正在呼叫Klaus Hartl cookie插件,由于某种原因,这并不是为我做的。

Much appreciated. 非常感激。

I took a look at the code for that plugin. 我看了看该插件的代码。 I think you might need to specify options and the expiry when you create the cookie. 我认为您在创建Cookie时可能需要指定选项和有效期。 Something like this: 像这样:

   var date = new Date();
   date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));
   $.cookie(COOKIE_NAME, 'test', { path: '/', expires: date });

Code ripped off the plugin site :) - http://stilbuero.de/jquery/cookie/ 代码从插件站点剥夺了:)-http: //stilbuero.de/jquery/cookie/

I would download the Web Developer toolbar for Firefox and use it to examine the cookies. 我将下载Firefox的Web Developer工具栏 ,并使用它检查Cookie。 Can you see it being made locally? 您能看到它在本地制作吗?

It could also be, if 'the_value' is something falsy, it won't work. 也可能是,如果'the_value'是虚假的,它将无法工作。 For example, if the value was '0', I think that would evaluate to false. 例如,如果该值为“ 0”,则我认为该值为假。

try 尝试

alert($.cookie('cookieName')); 

and see if it has a value on it. 看看它是否有价值。

or 要么

have you tried 你有没有尝试过

if($.cookie('cookieName') == null) { 
// do your thing
// then set cookie and put a great amount of days in the expire param
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM