This is a reposting of a previous question. Other question wasn't formulated good enough. I won't delete the other question, because it might be usable for others.
Hello All,
I have a javascript/jquery script, containing cookies and directing background-image-classes for the body tag, via<body id='body_bg'>
. Small portion of this script for example:
$('.somebuttons_class').live('click', function(){
$('#body_bg').removeClass('image10').addClass('image1');
$.cookie('bg_for_the_day', 'image1', { path: '/', expires: 1 });
});
In this piece of script the user is displaying an optional 'image10' to 'body_bg', but wants to change this back to the default image of 'image1'.
This script is now in the head section of my 'mypage.html' page. I would like to move this to an external js-page.
Problem is:
as you can see, I have called the default class for the body by name, 'image1'. But the default class for 'myotherpage.html' is 'image2' and for 'yetanother.html' it is 'image3'. This means I have to copy/past the whole script to 'myotherpage.html' and change all the 'image1's into 'image2's. That is a lot of dubble code, but it would work. However, as shown, my cookies give me back value='image1' as well. So that's not gonne work for any of my other pages at all!
Question: How do I set a variable of say 'default', instead of class='image1' or 'image2', in my javascript? And how do I input the script to translate 'default' back to 'image1' if the page is 'mypage.html', but use 'image2' if the page is 'myotherpage.html'?
Just to give you an impression of what I mean:
var currentPage = window.location.pathname;
var default;
if(currentPage == "mypage.html"){
default = "image1";
}
if(currentPage == "myotherpage.html"){
default = "image2";
}
Then the previous script is going to look something like this:
$('.somebuttons_class').live('click', function(){
$('#body_bg').removeClass('image10').addClass('default');
$.cookie('bg_for_the_day', 'default', { path: '/', expires: 1 });
});
I, by now have learned quite a bit about javascript, this feels like getting to the next level ;) I thank you in advance for your help.
Edit: After the answer I got I tried again. I didn't get it working. I decided to make a simplefied version of the actual script and post it here. Please check it out, if you have some time to spare.
You can download the testpage here. Every help is welcome!