检查cookie存在登录Logoff使用jquery / js [英] checking a cookie exists Login Logoff using jquery / js
问题描述
我有一个DNN登录/注销控件,无法正常工作。
当用户登录页面上的HTML时,显示如下:
< a href =javascript:__ doPostBack('dnn $ dnnLOGIN $ cmdLogin','')class =SkinObjectid =dnn_dnnLOGIN_cmdLogin>退出< / a>
并且当他们注销时,它看起来像这样:
< a href =javascript:__ doPostBack('dnn $ dnnLOGIN $ cmdLogin','')class =SkinObjectid =dnn_dnnLOGIN_cmdLogin>登录< / a>
-
我想检查Cookie是否已设置,如果它有然后
显示注销链接,如果它没有然后显示登录链接。 -
点击登录将检查是否存在Cookie(应显示为登录
),并将其转到登录页面。 -
点击Logoff应该删除该cookie并刷新页面
,然后将链接重新更改为登录,因为没有找到cookie。 / p>
我以此为例: http://jsfiddle.net/MadLittleMods/73vzD/
这是我到目前为止所做的: / p>
HTML:
< a id =dnn_dnnLOGIN_cmdLoginhref =登录>
登录
< / a>
||
< a id =dnn_dnnLOGIN_cmdLoginhref =Logoff>
Logoff
< / a>
< br />
< a id =seehref =#>
查看
< / a>
JS:
//在用户登录后设置cookie
//用于测试目的点击登录应将cookie设置为1
//点击Logoff '将cookie设置为0
$('#dnn_dnnLOGIN_cmdLogin')。live('click',function(){
var action = $(this).attr('href');
var cookie_value =(action =='Login')?1:null;
$ .cookie('DNN-COOKIE',cookie_value);
return false;
});
//点击'see'应该会出现一个警告框显示cookie值为1或0
$('#see')。live('click',function {
alert($。cookie('DNN-COOKIE'));
return false;
});
Cookie的演示工作正常。
看到一个工作这里
更新:
您可以检查已有的像这样的cookie
var preval = $ .cookie -名称');
if(preval!= null){
// ...
}
I have a DNN login/logoff control which isnt working properly. I want to therefore make my own using JS/JQUERY.
When the user is logged in the HTML on the page looks like this:
<a href="javascript:__doPostBack('dnn$dnnLOGIN$cmdLogin','')" class="SkinObject" id="dnn_dnnLOGIN_cmdLogin">Logout</a>
and when they are 'logged out' it looks like this:
<a href="javascript:__doPostBack('dnn$dnnLOGIN$cmdLogin','')" class="SkinObject" id="dnn_dnnLOGIN_cmdLogin">Login</a>
I would like to check if the cookie has been set, if it has then display Logoff link and if it hasnt then display the Login link.
Clicking on Login will check if the cookie exists (it should as Login was displayed) and take them to the login page.
Clicking on Logoff should delete the cookie and the refresh the page which will then change the link back to 'Login' again because no cookie was found.
I was using this as an example guide: http://jsfiddle.net/MadLittleMods/73vzD/
This is what i have done so far:
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
</a>
JS:
//set the cookie once user has logged in
//for testing purposes clicking login should set the cookie to 1
//clicking on Logoff 'should' set the cookie to 0
$('#dnn_dnnLOGIN_cmdLogin').live('click', function() {
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;
});
The demo of cookie is working just fine. Its seems you forgot to include the plugin.
See a working one here
Update:
You can check the preexisting of a cookie like this
var preval = $.cookie('cookie-name');
if(preval != null) {
//...
}
这篇关于检查cookie存在登录Logoff使用jquery / js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!