简体   繁体   English

设置jQuery Cookie的问题

[英]Trouble with setting jQuery cookie

First of all, sorry, I see there are lots of repetitions about the whole cookie question, yet I have to add this one for I am getting quite confused with different jQuery cookie plugIns. 首先,对不起,我看到关于整个cookie问题有很多重复,但是我不得不添加这个,因为我对不同的jQuery cookie插件感到非常困惑。

I am using this jQuery Cookie Plugin v1.4.1 我正在使用这个jQuery Cookie插件v1.4.1

I successfully implemented the following code on one of my pages (not the home page!) 我在我的一个页面(不是主页!)上成功实现了以下代码。

$(document).ready( function() { 
    if ($.cookie('noFadeWorks')) {
        // do something when initial effect on page enter has been already seen
    } else {
        // functions here to do the initial page enter effect
        $.cookie( 'noFadeWorks', true );    
    };
});

Now this basically works with some problems/questions yet: 现在,这基本上可以解决一些问题/问题了:

  1. Working as expected in Safari. 在Safari中按预期工作。 When browser is closed and reopened everything starts from 0 as expected and desired. 当浏览器关闭并重新打开时,所有内容均从0开始,符合预期和期望。 YET: Google Chrome even after close and reopen still has the cookie. 是的:即使关闭并重新打开后,Google Chrome仍然具有cookie。 My effect functions do not enter the game anymore. 我的效果功能不再进入游戏。 Not desired here. 这里不需要。

  2. It would be awesome if from some points of the session I could tell the browser to forget about the cookie and start from 0 (with the effect on the certain page) again. 如果从会话的某些方面来看,我可以告诉浏览器忘记cookie,然后再次从0开始(在特定页面上生效),那将是很棒的。 I tried for that: $.removeCookie( 'noFadeWorks' ); 我为此尝试:$ .removeCookie('noFadeWorks'); on my homepage but this didn't work at all. 在我的首页上,但这根本不起作用。

Do I explain myself? 我能自我解释吗? How is this to be done correctly? 如何正确地做到这一点? I also tried the expire options without success. 我也尝试了到期选项,但没有成功。

Thanks so much in advance! 非常感谢!

Note: jquery cookie v1.4.1 does not accept a Number as value: https://github.com/js-cookie/js-cookie/tree/v1.4.1 . 注意:jQuery cookie v1.4.1不接受数字作为值: https : //github.com/js-cookie/js-cookie/tree/v1.4.1 But that is not the problem. 但这不是问题。

I have created a small test case with jquery v2.1.4 and jquery-cookie v1.4.1 (from the link above): 我已经使用jquery v2.1.4和jquery-cookie v1.4.1创建了一个小测试用例(来自上面的链接):

<!DOCTYPE html>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script>
    $(document).ready( function() { 
        if ($.cookie('noFadeWorks')) {
            // do something when initial effect on page enter has been already seen
            alert( "has cookie!" );
        } else {
            // functions here to do the initial page enter effect
            $.cookie( 'noFadeWorks', 'true' );
            alert( "No cookie, creating..." );
        };
    });
</script>

The following occurs: 发生以下情况:

  1. When you enters the first time, it creates the cookie ("No cookie, creating...") 第一次输入时,它会创建cookie(“没有cookie,正在创建...”)
  2. When you refresh the page, the cookie exists ("has cookie!") 刷新页面时,该cookie存在(“有cookie!”)
  3. If you close and open the browser again, it says ("has cookie!"), the expected result is ("No cookie, creating...") because in v1.4.1 it should create a session cookie by default and we didn't specified path: "/" 如果您再次关闭并打开浏览器,它会显示(“ has cookie!”),则预期结果是(“ No Cookie,正在创建...”),因为在v1.4.1中它应默认创建一个会话cookie,而我们没有指定的path: "/"

If I use ctrl + shift + n to start a window in anonymous mode the cookie is removed (Chrome start a new clean browser instance). 如果我使用ctrl + shift + n以匿名模式启动窗口,则会删除Cookie(Chrome启动新的干净浏览器实例)。

So here is my theory: 所以这是我的理论:

If you have selected to restore the tabs after reopening Chrome, then the cookie is created again because Chrome is restoring it's window session, including all the cookies that existed previously. 如果您选择在重新打开Chrome后还原标签,则将再次创建cookie,因为Chrome正在还原其窗口会话,包括以前存在的所有cookie。 Probably if you disable the "restore session" feature of chrome, the cookies will be removed once the user closes the browser, as expected. 如果您禁用了chrome的“恢复会话”功能,则可能会在用户关闭浏览器后按预期方式删除cookie。

I didn't tested disabling that feature, but intuitively that seems to be the problem. 我没有测试禁用该功能,但是直觉上这似乎是问题所在。

It would be awesome if from some points of the session I could tell the browser to forget about the cookie and start from 0 (with the effect on the certain page) again. 如果从会话的某些方面来看,我可以告诉浏览器忘记cookie,然后再次从0开始(在特定页面上生效),那将是很棒的。

You can specify a path in which you want the cookie to be valid, the cookie will not be possible to be read from another path closer to the root (like / ): 您可以指定一个路径,在该路径中希望cookie有效, 不能从更接近根的另一个路径(如/ )中读取cookie:

<!DOCTYPE html>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script>
    $(document).ready( function() {
        if ($.cookie('noFadeWorks')) {
            // do something when initial effect on page enter has been already seen
            alert( "has cookie!" );
        } else {
            // functions here to do the initial page enter effect
            $.cookie( 'noFadeWorks', 'true', {
                // It will not be visible in the root, only in pages inside the "/dir/" path
                path: "/dir/"
            });
            alert( "No cookie, creating..." );
        };
    });
</script>

I recommend to try out the latest version of jquery-cookie (now js-cookie): https://github.com/js-cookie/js-cookie/releases 我建议尝试使用最新版本的jquery-cookie(现在为js-cookie): https : //github.com/js-cookie/js-cookie/releases

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

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