【问题标题】:How to fix undefined cookie value in Google Chrome?如何修复谷歌浏览器中未定义的 cookie 值?
【发布时间】:2015-10-15 10:23:20
【问题描述】:

我正在使用jQuery cookie plugin,来设置和存储模态的cookie,因此它只会在用户访问网站时显示一次。我测试了模态,它在最新版本的 Firefox、IE 和 Microsoft Edge 中都能正常工作,但是,谷歌浏览器无法正常工作,并且在每次页面刷新时都会反复调出模态。我创建了一个警报来读取所述 cookie 的值:

alert($.cookie("modal_shown")); // read available cookie

我在控制台调试了一下,发现上面的代码返回如下:

火狐:1
Internet Explorer:yes
Microsoft Edge:yes
谷歌浏览器:undefined
歌剧:undefined

话虽如此,使用浏览器控制台,我试图弄清楚为什么最后两个浏览器给了我undefined 的值,而其他浏览器工作正常?有什么解决办法吗?

这是我的代码:

HTML:

<a href="#openModal">Open Modal</a>
<div id="openModal" class="modalDialog">
    <div>   
        <h2>Notice!</h2>
        <div class="control_toggle_icon"></div>
        <p style="text-align:left; margin: 0 0 12px;">Toggle this menu icon to show/hide the menu bar and view the images in full screen.</p>
        <a href="#close" title="Close" class="modal_btn shortcode_button btn_small btn_type1">Okay, Got it!</a>
    </div>
</div>

模态与我在jsfiddle sn-p 中使用的基本相同。

JS:

<script type="text/javascript" src="js/jquery.cookie.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
    "use strict";
    // only show modal once and hide it until expired
    if ($.cookie('modal_shown') == null) 
    {
        $.cookie('modal_shown', 'yes', { expires: 365, path: '/' });
        // show modal
        $(".modalDialog").css({opacity : "1", pointerEvents : "auto"});
    }

    // destroy modal upon click
    $(".modal_btn").click(function(){
        $(".modalDialog").css({opacity : "0", pointerEvents : "none"});
    });
    alert($.cookie("modal_shown")); // read available cookie
});
</script>

更新的测试代码:

<script type="text/javascript">
jQuery(document).ready(function(){
    "use strict";
    console.log(cookieVal); // undefined
    var cookieVal = $.cookie("modal_shown") || "no";
    console.log(cookieVal); // "no", "1" for firefox, "yes" for ie, me

    // only show modal once and hide it until expired
    if (cookieVal !== "yes")
    {
        console.log(cookieVal); // "no", "1" for firefox, "yes" for ie, me
        $.cookie('modal_shown', 'yes', { expires: 365, path: '/' }); // here it doesn't change the value to yes until next line
        cookieVal= "yes"; // force value to become yes from no
        // show modal
        $(".modalDialog").css({opacity : "1", pointerEvents : "auto"});
        console.log(cookieVal); // "yes"
    }

    // destroy modal upon click
    $(".modal_btn").click(function(){
        $(".modalDialog").css({opacity : "0", pointerEvents : "none"});
        console.log(cookieVal); // "yes"
    });
});
</script>

解决方案:必须将代码上传到本地或在线服务器,才能使 jquery cookie 完全按预期工作。它现在适用于所有现代浏览器版本。模式将在页面刷新或关闭/重新打开浏览器时显示一次且不再显示(或直到设置的到期日期,在本例中为 365 天)。

【问题讨论】:

    标签: javascript jquery cookies undefined jquery-cookie


    【解决方案1】:

    如果 cookie 值不存在,则 $.cookie() 将返回 undefined。因此,您可以测试该特定值,也可以指定一个默认值。

    如果你想分配一个默认值并且你的期望值是"no""yes"undefined,那么你可以这样做:

    var cookieVal = $.cookie("modal_shown") || "no";
    

    可以这样合并:

    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function(){
        "use strict";
        // only show modal once and hide it until expired
        var cookieVal = $.cookie("modal_shown") || "no";
        console.log(cookieVal);      // log cookieVal
        if (cookieVal !== "yes") {
            $.cookie('modal_shown', 'yes', { expires: 365, path: '/' });
            // show modal
            $(".modalDialog").css({opacity : "1", pointerEvents : "auto"});
        }
    
        // destroy modal upon click
        $(".modal_btn").click(function() {
            $(".modalDialog").css({opacity : "0", pointerEvents : "none"});
        });
    });
    </script>
    

    【讨论】:

    • 嘿,我记得你的名字!所以,在我的代码中,我会这样做:if ($.cookie('modal_shown') == null || $.cookie('modal_shown') == undefined) {...}?我试过了,但仍然未定义。
    • @TheAmazingKnight - 您的if 声明根本不会影响您的alert()。这就是为什么没有任何改变。我在答案中添加了代码建议。
    • 我知道你在那里做了什么。我更新了上面的代码并尝试将其正确处理,但是从 cookie 传递的“yes”值没有改变,我添加了一个条件来强制改变值,如果它不是“yes”价值,但模式不断出现,现在所有浏览器都在这样做。插件似乎没有正确呈现,或者我做错了我看不到的事情。
    • 解决方案:我把它上传到本地服务器,看看它是否注册,它确实有效!我想离线使用 cookie 是行不通的,但上传到本地服务器可以。那很有意思。感谢您的帮助,我真的很感激! :)
    • @TheAmazingKnight - 在使用file:// URL 时,每个浏览器对于何时存储 cookie 都有不同的规则 - 行为不是标准的。 Chrome 需要特殊配置才能执行此操作。其他浏览器默认会这样做。我想这是我们不知道您使用file:// URL 的事情之一。不管怎样,很高兴你明白了。见stackoverflow.com/questions/6232331/…
    猜你喜欢
    • 2012-10-06
    • 2015-04-06
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-27
    • 2019-11-22
    • 2016-08-31
    相关资源
    最近更新 更多