...

View Full Version : Object Handling Headache



PettyRider
10-31-2004, 04:03 AM
Okay, I'm trying to do this random image thing with two images. Here is the jist of everything:


<head>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
var img_header = new Array("header_1.jpg", "header_2.jpg",
"header_3.jpg", "header_4.jpg");

var img_main= new Array("main_1.jpg", "main_2.jpg",
"main_3.jpg", "main_4.jpg");

var l = img_header.length;

var rnd_no = Math.round((l-1)*Math.random());

document.header.src = "img_header[rnd_no]";
document.main.src = "img_main[rnd_no]";

//-->
</SCRIPT>
</head>
<body>
<img src="header.jpg" name="header">
<img src="main.jpg" name="main">
</body>

I just get the error "'document.header' is null or not an object" and "'document.main' is null or not an object," but they are objects. They're images are they not?

I need to do this so the random images correspond with each other. Any other ways to do it? This is driving me crazy.

PettyRider
10-31-2004, 05:06 AM
Well, I just took a different route with it and it worked:


<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
// Set up the image files to be used.
var theImages = new Array()
theImages[0] = 'header_1.jpg'
theImages[1] = 'header_2.jpg'

var theMain = new Array()
theMain[0] = 'main_1.jpg'
theMain[1] = 'main_2.jpg'

// do not edit anything below this line

var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}
function showImage2(){
document.write('<img src="'+theMain[whichImage]+'">');
}

// End -->
</script>

Then I called showImage() and showImage2() where I needed them.

SpiritualStorms
10-31-2004, 10:53 AM
Excuse me, Mr Petty Rider, but what exactly do you plan to do with this piece of code:



var j = 0
?

Seems like a mighty lonely fellow, if you ask me.

liorean
10-31-2004, 04:46 PM
PettyRider: You can probably use your first script, with a change of a few details:

document.header --> document.getElementById('header')

<img src="header.jpg" name="header"> --> <img src="header.jpg" id="header">

"img_header[rnd_no]"; --> img_header[rnd_no];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum