开发者

Javascript conditionals based on cookies

开发者 https://www.devze.com 2023-03-03 02:06 出处:网络
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:

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

精彩评论

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