简体   繁体   English

检查是否设置了cookie,然后使用jquery cookie库显示注销链接

[英]checking if a cookie is set then display logoff link using jquery cookie library

I am trying to clear a DNN cookie using the cookie.js library. 我正在尝试使用cookie.js库清除DNN cookie。 When the user clicks login they should login via the CMS. 当用户单击登录时,他们应该通过CMS登录。 then the jquery needs to: 然后,jQuery需要:

check if the cookie is set (if its set then hide the login link and display logoff) 检查cookie是否已设置(如果已设置,则隐藏登录链接并显示注销)

Here is my attempt: 这是我的尝试:

HTML: 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: 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;
});

I have added the library resource to this JsFiddle: http://jsfiddle.net/whQaq/ I need to check if the cookie is set 我已将库资源添加到此JsFiddle中: http : //jsfiddle.net/whQaq/我需要检查cookie是否已设置

EDIT: here is an example which seems to be working but in DNN when i place the code in the skin file it doesnt work. 编辑:这是一个似乎正在工作的示例,但是在DNN中,当我将代码放置在皮肤文件中时,它不起作用。 http://jsfiddle.net/whQaq/3/ http://jsfiddle.net/whQaq/3/

I would test the following: 我将测试以下内容:

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

then retrieve: 然后检索:

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

and assert that: 并断言:

preval === null;

In any case, it's probably safer to use: 无论如何,使用它可能更安全:

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

UPDATE: 更新:

Could it be you need to set the cookie to your domain's root: 您可能需要将cookie设置为您域的根目录:

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

That might be the case if you're using subdomains. 如果您使用的是子域,那就可能是这种情况。

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

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