簡體   English   中英

檢查是否設置了cookie,然后使用jquery cookie庫顯示注銷鏈接

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

我正在嘗試使用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/

我將測試以下內容:

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

然后檢索:

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

並斷言:

preval === null;

無論如何,使用它可能更安全:

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

更新:

您可能需要將cookie設置為您域的根目錄:

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

如果您使用的是子域,那就可能是這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM