开发者

Setting Cookie to Affect Styles / Calling Function After DOM Ready

开发者 https://www.devze.com 2023-02-25 02:59 出处:网络
Got a system setup to swap style sheets when a user clicks on a button. Cookie is set. jQuery(document).ready(function() {

Got a system setup to swap style sheets when a user clicks on a button. Cookie is set.

jQuery(document).ready(function() {    

$(function() { 
            $("#designSwap li a").click(function() { 
                $("link").attr("href",$(this).attr('rel'));            
                $.cookie("cssSessionColor",$(this).attr('class'), {expires:null, path: '开发者_如何学JAVA/'});
                $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
                updateColor($(this).attr('class'));
                return false;
            });
        });
});

Otherwise, if a user hasn't set a preference between style sheets, picks one at random and sets just a session cookie:

if($.cookie("css")) {       
    $("link").attr("href",$.cookie("css"));

}else if($.cookie("cssSession")) {
    $("link").attr("href",$.cookie("cssSession"));

}else{

    var rand = Math.floor(Math.random()*2)

    if (rand > 0) {

       $("link").attr("href","/lib/css/common-dark.css");
       $.cookie("cssSessionColor","dark", {expires:null, path: '/'});
       $.cookie("cssSession","/lib/css/common-dark.css", {expires:null, path: '/'});


    }else{

       $("link").attr("href","/lib/css/common.css");
       $.cookie("cssSessionColor","light", {expires:null, path: '/'});
       $.cookie("cssSession","/lib/css/common.css", {expires:null, path: '/'});

    };
}

All of that is working fine. Problem lies in setting a color for the Flash video player on the site. (updateColor function). It works if the user clicks on the links to pick a color, but isn't working by just reading the cookie.

We think the problem is it's firing before the DOM is ready.

What we are trying to do is to ensure that we calling the adjust color once the ready function hits. This is most likely an easy thing, we just aren't seeing it.


You have:

$(function() { });

nested inside:

jQuery(document).ready(function() { });

These mean the same thing with the former being the short-hand of the latter. I would bet this is causing your issue as you're binding an anonymous function to document.ready on document.ready. Document.ready has thus already happened and won't call the function.

EDIT: I thought about it more and it seems I might be wrong.... Never did it (no reason to), but this actually does work and thus isn't your issue. I'll leave the post here though so that you can correct it. :)

http://jsfiddle.net/bcEQZ/

0

精彩评论

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