0

特定のドメインに myCookie を設定しています。Cookie が設定されている場合、コンテンツを非表示にしたいのですが、IF ステートメントの表示/非表示がまったく機能しません。

jquery .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

これでどんな助けでも大歓迎です!

4

2 に答える 2

2

私はあなたに例を作りました:

<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>

この例では、25 > 0 であるため、div を非表示にしています。

その他の場合:

<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>

ここで、if 条件と、それが myCookie = 25 を示している div を変更し、それは 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

クッキーの読み取り:

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=/";

サルドス ;)

于 2013-03-27T16:07:56.240 に答える
1

まず、https://github.com/carhartl/jquery-cookieをダウンロードし、ページで参照します。

次に、ifステートメントを次のように変更するだけです。

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

$.cookie必要に応じて、Cookie の設定にも使用できます。README.md に例があります。

于 2013-03-27T15:58:46.833 に答える