开发者

Javascript conditionals based on cookies

Im trying to set up some conditional statements that detect whether or not a cookie has been set when the page loads and applies some classes to a div based on the value of that cookie:

<script>
        $(function(){   
            if ($.cookie('view_size', 'large')) {
                $('#primary').removeClass('medium_content');
                $('#primary').addClass('large_content');
            };

            if ($.cookie('view_size', 'medium')) {
                $('#primary').removeClass('large_content');
                $('#primary').addClass('medium_content');
            };

            if ($.cookie('view_size', 'small')) {
   开发者_运维技巧             $('#primary').removeClass('large_content');
                $('#primary').removeClass('medium_content');
            };
        });
        </script>

I am using the jquery cookie plugin and I know the cookies are getting successfully set, so the issue must have to do with the way I have set up these conditional statements. Any ideas?


I believe that the syntax

$.cookie('view_size', 'large')

is the way that you create a cookie, and isn't meant to check the value of a cookie. I believe you want

if ($.cookie('view_size')=='large')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜