简体   繁体   中英

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. When the user clicks login they should login via the CMS. then the jquery needs to:

check if the cookie is set (if its set then hide the login link and display logoff)

Here is my attempt:

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

I have added the library resource to this JsFiddle: http://jsfiddle.net/whQaq/ I need to check if the cookie is set

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. 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('the_cookie', 'the_value', { path: '/' });

That might be the case if you're using subdomains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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