开发者

jQuery custom background through a cookie

I'm making background changing script for my site. Everything works fine except the cookie which should store the required value. I'm using jQuery 1.3. IE 8 says: 'object doesn't support this property or method on line 47 char 118567432'!? Any help will be greatly appreciated. Working example without cookie:

    function images(which,image) {
      if (which == 't1')image = images[0];
      else if (which == 't2')image = images[1];//and etc...
    }
    $('html').css("background-image", image);

Example with cookie 开发者_Python百科(not working):

function images(which,image) {
          if (which == 't1')image = images[0];
            {$.cookie("html_img", "" + image + "", { expires: 7 });
            imgCookie = $.cookie("html_img");}
          else if (which == 't2')image = images[1];
            {$.cookie("html_img", "" + image + "", { expires: 7 });
            imgCookie = $.cookie("html_img");}
          }
        $('html').css("background-image", imgCookie);


I've converted your code to a more efficient, and syntactically valid JavaScript code.

function images(which){
    var image = /^t\d+/i.test(which) ? images[which.substr(1)-1] : null;
    if(image !== null) {
        $.cookie("html_img", image, {expires:7})
    } else {
        image = $.cookie("html_img");
        if(!image) image = images[0]; //If the image doesn't exist, use default=0
    }
    $('html').css("background-image", image);
}

$(document).ready(function(){
    images(); //Load image default from cookie
}

//If you want to change the image+cookie: images("t2");


May be your "cookie plugin" script is not imported correctly? Can you give more details on the error you get. Use Firebug for Firefox or Chrome Dev tools to get a better error trace.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜