【问题标题】:checking if a cookie is set then display logoff link using jquery cookie library检查是否设置了 cookie,然后使用 jquery cookie 库显示注销链接
【发布时间】:2012-04-24 15:00:08
【问题描述】:

我正在尝试使用 cookie.js 库清除 DNN cookie。 当用户单击登录时,他们应该通过 CMS 登录。那么jquery需要:

检查是否设置了cookie(如果设置了则隐藏登录链接并显示注销)

这是我的尝试:

HTML:

<a id="dnn_dnnLOGIN_cmdLogin" href="Login">
    Login
</a>

||

<a id="dnn_dnnLOGIN_cmdLogin" href="Logoff">
    Logoff
</a>

<br />

<a id="see" href="#">
    see if cookie is set?
</a>

JQUERY:

$('#dnn_dnnLOGIN_cmdLogin').live('click', function() {
    var preval = $.cookie('DNN-COOKIE');
    if(preval != null) {
        $(this).hide();
    } else {
    var action = $(this).attr('href');
    var cookie_value = (action == 'Login') ? 1 : null;

    $.cookie('DNN-COOKIE', cookie_value);
    }
    return false;
});

// clicking on 'see' should bring up an alert box display the cookie value of 1 or 0
$('#see').live('click', function() {

    alert($.cookie('DNN-COOKIE'));

    return false;
});

我已将库资源添加到此 JsFiddle:http://jsfiddle.net/whQaq/ 我需要检查是否设置了cookie

编辑:这是一个似乎可以工作的示例,但在 DNN 中,当我将代码放在皮肤文件中时它不起作用。 http://jsfiddle.net/whQaq/3/

【问题讨论】:

    标签: javascript jquery cookies


    【解决方案1】:

    我会测试以下内容:

    $.cookie('DNN-COOKIE', null);
    

    然后检索:

    var preval = $.cookie('DNN-COOKIE')
    

    并断言:

    preval === null;
    

    无论如何,使用它可能更安全:

    if(!preval) {
        $(this).hide();
    }
    

    更新:

    您是否需要将 cookie 设置为您域的根目录:

    $.cookie('the_cookie', 'the_value', { path: '/' });
    

    如果您使用子域,情况可能就是这样。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 2023-03-15
    相关资源
    最近更新 更多