次のJSコードを見つけました(私のものではありません。クレジットはMichael Regan、www.owt4nowt.caに帰属します)
したがって、コード:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
var key_value = "myTestCookie=true";
var foundCookie = 0;
// Get all the cookies from this site and store in an array
var cookieArray = document.cookie.split(';');
for(var i=0;i < cookieArray.length;i++)
{
var checkCookie = cookieArray[i];
while (checkCookie.charAt(0)==' ')
{
checkCookie = checkCookie.substring(1,checkCookie.length);
}
if (checkCookie.indexOf(key_value) == 0)
{
alert("Found Cookie ");
foundCookie = 1;
}
}
if ( foundCookie == 0)
{
document.cookie = key_value;
alert("Setting Cookie");
}
</script>
アラートは確実に機能します。
ただし、Cookie が認識されたときにアラートを削除しました。
if (checkCookie.indexOf(key_value) == 0)
{
foundCookie = 1;
}
そして、「Setting Cookie」アラートを addClass 関数に置き換えたか、そう思いました。
if ( foundCookie == 0)
{
document.cookie = key_value;
$('.someClassHere').addClass("newClass");
}
問題は、機能がまったく機能しないことです。アラートだけにしておけば問題ないのですが、addClass関数は完全無視です。
addClass 関数に構文エラーがありますか?