检查cookie存在使用jquery / js登录注销

我有一个DNN登录/注销控件,它无法正常工作。 因此,我想使用JS / JQUERY进行自己的操作。

当用户登录时,页面上的HTML如下所示:

Logout 

当它们“退出”时,它看起来像这样:

 https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login 
  • 我想检查cookie是否已设置,是否已显示https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Logoff链接,如果没有,则显示https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login链接。

  • 单击https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login将检查cookie是否存在(它应该显示为https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login)并将其带到登录页面。

  • 单击https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Logoff应该删除cookie并刷新页面,然后再次将链接更改回“https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login”,因为没有找到cookie。

我使用它作为示例指南: http : //jsfiddle.net/MadLittleMods/73vzD/

这是我到目前为止所做的:

HTML:

  https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login  ||  https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Logoff  
see

JS:

 //set the cookie once user has logged in //for testing purposes clicking login should set the cookie to 1 //clicking on https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Logoff 'should' set the cookie to 0 $('#dnn_dnnLOGIN_cmdhttps://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/Login').live('click', function() { var action = $(this).attr('href'); var cookie_value = (action == 'https://stackoverflow.com/questions/10102483/checking-a-cookie-exists-login-logoff-using-jquery-js/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; }); 

cookie的演示工作得很好。 它似乎忘了包含插件。

在这看一个工作


更新:

你可以检查这样一个cookie的预先存在

 var preval = $.cookie('cookie-name'); if(preval != null) { //... } 

演示