couppi
01-06-2007, 04:08 AM
I've been trying to create a script to change the background image of my site and store it in a cookie. This is what I have so far:
<script language="javascript">
<!--
var backImage = new Array();
back[0] = "aquablack.jpg";
back[1] = "aquablue.jpg";
back[2] = "aquared.jpg";
function changebgimage(backnum){
if (document.body){
document.body.background = back[backnum];
}
}
function cookie(number){
if (document.body){
document.cookie = "background="+number+";expires=Fri, 17 Dec 2010 12:00:00 GMT";
}
}
function bgimage(somenumber){
if (document.body){
changebgimage(somenumber);
cookie(somenumber);
}
}
function ReadCookie(cookieName) {
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName);
if (ind==-1 || cookieName=="") return "";
var ind1=theCookie.indexOf(';',ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
var bg = ReadCookie(background);
function initialbackground() {
if (document.body){
changebgimage(bg);
}
}
//-->
</script>
Right after the <body> tag, I have this:
<script language="javascript">
initialbackground();
</script>
I copied the ReadCookie function, so I have no idea how it works. The bgimage function changes the background and sets the cookie, but the initialbackground function doesn't seem to work. What's wrong with the script?
<script language="javascript">
<!--
var backImage = new Array();
back[0] = "aquablack.jpg";
back[1] = "aquablue.jpg";
back[2] = "aquared.jpg";
function changebgimage(backnum){
if (document.body){
document.body.background = back[backnum];
}
}
function cookie(number){
if (document.body){
document.cookie = "background="+number+";expires=Fri, 17 Dec 2010 12:00:00 GMT";
}
}
function bgimage(somenumber){
if (document.body){
changebgimage(somenumber);
cookie(somenumber);
}
}
function ReadCookie(cookieName) {
var theCookie=""+document.cookie;
var ind=theCookie.indexOf(cookieName);
if (ind==-1 || cookieName=="") return "";
var ind1=theCookie.indexOf(';',ind);
if (ind1==-1) ind1=theCookie.length;
return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
var bg = ReadCookie(background);
function initialbackground() {
if (document.body){
changebgimage(bg);
}
}
//-->
</script>
Right after the <body> tag, I have this:
<script language="javascript">
initialbackground();
</script>
I copied the ReadCookie function, so I have no idea how it works. The bgimage function changes the background and sets the cookie, but the initialbackground function doesn't seem to work. What's wrong with the script?