JamesGreen
04-18-2009, 03:55 PM
Hi everyone,
I guess this may have been a question before but I couldn't search for it because the term "if" was too small...
So I'm a really big noob, started javascript today, looked everywhere and ended up here.
I have this javascript,
if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=2;
rndimg = new Array("./Images/image_01.jpg", "./Images/image_02.jpg");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("image_01").style.backgroundImage = "url("+ randomimage +")";
}
if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=2;
rndimg = new Array("./Images/image_03.jpg", "./Images/image_04.jpg");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("image_02").style.backgroundImage = "url("+ randomimage +")";
}
But the problem is, the second if gets called the last in the html and so the first one isn't used when displayed (Hope I can explain it clearly)
So how can I get rid of the "if" things?
Oh BTW, the lines who uses javascript in my html file are
<a href="./iSoftware.html" id="image_01"></a>
<a href="./Contribute.html" id="image_02"></a>
and CSS:
#image_01 {
position: absolute;
background-image:url("../Images/image_01.png);
top:105;
left:52;
width:249px;
height:150px;
float:left;
}
#image_02 {
position: absolute;
background-image:url("../Images/image_03.png);
top:105;
left:302;
width:249px;
height:150px;
float:left;
}
Thanks
I guess this may have been a question before but I couldn't search for it because the term "if" was too small...
So I'm a really big noob, started javascript today, looked everywhere and ended up here.
I have this javascript,
if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=2;
rndimg = new Array("./Images/image_01.jpg", "./Images/image_02.jpg");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("image_01").style.backgroundImage = "url("+ randomimage +")";
}
if (document.getElementById) { window.onload = swap };
function swap() {
var numimages=2;
rndimg = new Array("./Images/image_03.jpg", "./Images/image_04.jpg");
x=(Math.floor(Math.random()*numimages));
randomimage=(rndimg[x]);
document.getElementById("image_02").style.backgroundImage = "url("+ randomimage +")";
}
But the problem is, the second if gets called the last in the html and so the first one isn't used when displayed (Hope I can explain it clearly)
So how can I get rid of the "if" things?
Oh BTW, the lines who uses javascript in my html file are
<a href="./iSoftware.html" id="image_01"></a>
<a href="./Contribute.html" id="image_02"></a>
and CSS:
#image_01 {
position: absolute;
background-image:url("../Images/image_01.png);
top:105;
left:52;
width:249px;
height:150px;
float:left;
}
#image_02 {
position: absolute;
background-image:url("../Images/image_03.png);
top:105;
left:302;
width:249px;
height:150px;
float:left;
}
Thanks