2013-03-27 56 views
0

我为特定域设置了myCookie,并且在设置cookie时我想隐藏内容,但显示/隐藏IF语句完全不起作用。根据设定的Cookie隐藏内容

我想使用jQuery的.cookie插件,但这是我到目前为止。写入cookie确实有效,我无法让隐藏功能正常工作。

var cookieName = 'myCookie'; 
var cookieValue = 'myCookie'; 
var myDate = new Date(); 
myDate.setMonth(myDate.getMonth() + 12); 
document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate 
        + ";domain=.example.com;path=/"; 

$(document).ready(function(){ 
    if ($.cookieName('myCookie') >= 0) { 
     $('.myContent').hide(); 
    } 
}); 

HTML

<div class="myContent"> 
hide this content if the cookie is set 
</div> 
otherwise show this content if you don't have the required cookie 

在任何帮助,这将不胜感激!

+0

你使用任何的jQuery插件的cookie?否则这个'$ .cookieName'可能会出现错误或者不确定 – JFK 2013-03-27 15:49:54

+0

我不是,但我想......只是试图让它工作 – Evan 2013-03-27 15:55:21

回答

1

我给你做一个例子:

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="jquery.cookie.js"></script> 
<script type="text/javascript"> 
$.cookie('myCookie', 25); 

$(document).ready(function(){ 

    if ($.cookie('myCookie') >= 0) { 
     $('.myContent').hide(); 
    } 
}); 
</script> 
</head> 
<body> 
<div class="myContent"> 
hide this content if the cookie is set 
</div> 
</body> 
</html> 

这个例子隐藏DIV,因为25> 0

在其他情况下:

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="jquery.cookie.js"></script> 
<script type="text/javascript"> 
$.cookie('myCookie', 25); 

$(document).ready(function(){ 

    if ($.cookie('myCookie') >= 26) { 
     $('.myContent').hide(); 
    } 
}); 
</script> 
</head> 
<body> 
<div class="myContent"> 
hide this content if the cookie is set 
</div> 
</body> 
</html> 

现在我改变,如果条件和它显示的分区 myCookie = 25,它小于26(所以它在两种情况下工作)。

------------------------------编辑--------------- -------------------

的JavaScript版本:

function createCookie(name,value,days) { 
if (days) { 
    var date = new Date(); 
    date.setTime(date.getTime()+(days*24*60*60*1000)); 
    var expires = "; expires="+date.toGMTString(); 
} 
else var expires = ""; 
document.cookie = name+"="+value+expires+"; path=/"; //replace this line 

} 

function readCookie(name) { 
var nameEQ = name + "="; 
var ca = document.cookie.split(';'); 
for(var i=0;i < ca.length;i++) { 
    var c = ca[i]; 
    while (c.charAt(0)==' ') c = c.substring(1,c.length); 
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); 
} 
return null; 
} 

function eraseCookie(name) { 
createCookie(name,"",-1); 
} 

的JavaScript创建的cookie电话:

createCookie('ppkcookie','testcookie',7) //name_of_cookie,value,num_days 

读的cookie:

var x = readCookie('ppkcookie1') 

充分说明在这里:http://www.quirksmode.org/js/cookies.html

在的createCookie功能:

document.cookie = name+"="+value+expires+"; path=/"; //replace this line 

    //with this one adding domain 
    document.cookie = name+"="+value+expires=" + ";domain=.example.com;path=/"; 

Saludos;)

+0

非常感谢你!这真的很棒。顺便说一下,使用我提供的设置cookie的例子,如何在不使用jquery和插件的情况下完成相同的效果? – Evan 2013-03-27 20:22:34

+0

你的意思是用普通的javascript? – Hackerman 2013-03-27 20:29:01

+0

是的,无需使用插件。我很好奇基于我最初的方法它会是什么样子 – Evan 2013-03-27 20:35:11

1

首先,下载https://github.com/carhartl/jquery-cookie并在您的页面中引用。

然后,很简单,如改变你的if声明:

if ($.cookie("myCookie") !== undefined) 
{ 
    $(".myContent").hide(); 
} 

如果你愿意,你可以使用$.cookie设置Cookie过; README.md有例子。