Jquery - load cookie data
Consider a list of thumbnails from which the user clicks one to change the background image. The following scripts lets the user choose a background image and apply it.
$(function() {
$('.themes li a img').click(function() {
var image = 'imgs/someimage.jpg';
//Set cookie
function setCookie(){
$.cookie("html_img", image, {expires:7});
var a = $.cookie('html_img');//get cookie value
return a;
}
});
});
Another script called 开发者_运维知识库loadCookie.js
loads the cookie which contains the chosen background when the user visits another page:
$(function() {
var startImage='';
startImage = setCookie();
$(document).ready(function(){
$('html').css("background", startImage);
});
});
IE8 says 'object doesn't support this action, line 3'; Jquery cookie plugin is installed. I know there is some little mistake I can't find. Any help making the script work is greatly appreciated.
Not 100% on what you're trying to do, but I think this is what you're after (assuming you're using jquery.cookie.js):
$(function() {
if($.cookie("html_img")) {
$('html').css("background", $.cookie("html_img"));
}
$('.themes li a img').click(function() {
var image = 'imgs/someimage.jpg';
// var image = $(this).attr('src');
$('html').css("background", image);
$.cookie("html_img", image, {expires:7});
return false;
});
});
Since this is your third question posted about this same code (previous ones here and here), you seem really confused both about this code and about the proper way to use stack overflow.
Here's how cookies work in general. On one page, you set a cookie. On a follow-on page you read that cookie and use it's value.
In this code example, you are setting a cookie in the first block of code to a background image. So far, so good.
In the second block of code, you are calling setCookie again. If you want to retrieve the background image from the cookie, then you need to get the cookie value, not set it. Your code should probably be something like this:
$(document).ready(function() {
// initialize background from cookie
var startImage = $.cookie("html_img"); // get cookie value
if (startImage) {
$(document.body).css("background-image", startImage);
}
// install click handler to change background
$('.themes li a img').click(function() {
var newBackground = 'imgs/someimage.jpg';
$(document.body).css("background-image", newBackground);
$.cookie("html_img", newBackground, {expires:7}); // save in cookie
});
});
You may also need to know that if you do not specify a path option for the cookie like path: '/'
, the cookie will only be readable on the current path and not elsewhere on your site. Documentation for that jQuery add-on cookie library is here.
As for using stackoverflow, you should ask a clear question about a particular piece of code and when respondents are confused or provide answers that aren't what you meant to ask, you should clarify your question appropriately until you get an appropriate answer. Remember, when people provide answers in the wrong direction, it is most likely because they don't understand what you are asking, not because they don't know how to answer. You should not post a minor variation of the same question all over again in a new question.
精彩评论