473,583 Members | 4,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

randomly change background image with css and javascript?

Hello All,

I am trying to randomly change the background image of my home page
but I can't seem to figure it out.

Here is a snippet of my css

....
BODY {background:tra nsparent url(../images/homepagebg3.jpg ) no-repeat;
font:normal 90% Arial, Helvetica, sans-serif; color:#263158;}
....

Here is my javascript that I am trying to use to change my background
image....
<link type="text/css" href="css/main.css" media="screen"
rel="stylesheet " />
<link type="text/css" href="css/imagetitles.css " media="screen"
rel="stylesheet " />
<script language="javas cript" type="text/javascript">

images = new Array(3);
images[0] = 'background:tra nsparent url(../images/homepagebg3.jpg )
no-repeat; font:normal 90% Arial, Helvetica, sans-serif;
color:#263158;' ;
images[1] = 'background:tra nsparent url(../images/homepagebg1.jpg )
no-repeat; font:normal 90% Arial, Helvetica, sans-serif;
color:#263158;' ;
images[2] = 'background:tra nsparent url(../images/homepagebg2.jpg )
no-repeat; font:normal 90% Arial, Helvetica, sans-serif;
color:#263158;' ;

index = Math.floor(Math .random() * images.length);
document.getEle mentsByTagName( "BODY")[0].style.backgrou nd =
images[index];
</script>
Here is the error from the firefox javascript console:
Error: document.getEle mentsByTagName( "BODY")[0] has no properties.

Any ideas? How can I randomly change my background image?

Thanks!
--
Nathan
Jul 20 '05 #1
4 44312
Nathan Given wrote:
Hello All,

I am trying to randomly change the background image of my home page
but I can't seem to figure it out.

Here is a snippet of my css

...
BODY {background:tra nsparent url(../images/homepagebg3.jpg ) no-repeat;
font:normal 90% Arial, Helvetica, sans-serif; color:#263158;}
...

Here is my javascript that I am trying to use to change my background
image....
<link type="text/css" href="css/main.css" media="screen"
rel="stylesheet " />
<link type="text/css" href="css/imagetitles.css " media="screen"
rel="stylesheet " />
<script language="javas cript" type="text/javascript">

images = new Array(3);
images[0] = 'background:tra nsparent url(../images/homepagebg3.jpg )
no-repeat; font:normal 90% Arial, Helvetica, sans-serif;
color:#263158;' ;
images[1] = 'background:tra nsparent url(../images/homepagebg1.jpg )
no-repeat; font:normal 90% Arial, Helvetica, sans-serif;
color:#263158;' ;
images[2] = 'background:tra nsparent url(../images/homepagebg2.jpg )
no-repeat; font:normal 90% Arial, Helvetica, sans-serif;
color:#263158;' ;

index = Math.floor(Math .random() * images.length);
document.getEle mentsByTagName( "BODY")[0].style.backgrou nd =
images[index];
</script>
Here is the error from the firefox javascript console:
Error: document.getEle mentsByTagName( "BODY")[0] has no properties.

Any ideas? How can I randomly change my background image?

Thanks!
--
Nathan


You can't assign this in the CSS way since it is javascript and not CSS.
In this document there appears to be no javascript property for this
(don't know if they are right):
http://www.comptechdoc.org/independe...javastyle.html
A way to achieve this is to make two classes in CSS and assign a new
class to the object.
You can see how you can assign a class using javascript in my tabular
navigation example at: http://www.top100nederland.net/test/tabjes.htm

regards,

Roderik

--
http://www.archytas.nl/
webdesign, internet applicaties, internetgestuur de elektronica
Jul 20 '05 #2
Nathan Given wrote:
Here is my javascript that I am trying to use to change my background
image....

images = new Array(3); [snip] </script>

Here is the error from the firefox javascript console:
Error: document.getEle mentsByTagName( "BODY")[0] has no properties.


If you have a JavaScript question, ask in a JavaScript group. I
recommend comp.lang.javas cript.

--
Brian (remove ".invalid" to email me)
http://www.tsmchughs.com/
Jul 20 '05 #3
Brian wrote:
Nathan Given wrote:

<snip>
Here is the error from the firefox javascript console:
Error: document.getEle mentsByTagName( "BODY")[0] has no
properties.


If you have a JavaScript question, ask in a JavaScript
group. I recommend comp.lang.javas cript.


Fair enough, but the explanation of the error is fairly simple. A script
executed inline within the HEAD section of a page executes prior to the
HTML parser encountering (or implying) the opening BODY tag. As a result
there is no BODY element within the DOM to refer to and -
document.getEle mentsByTagName( "BODY") - must return an empty nodeList at
that point (so there is no item at index zero within that nodeList).

Richard.
Jul 20 '05 #4
Thank you all.

It turns out two problems:

First, Richard was right.

Second, I had the wrong path to my images. Wow, I'm dumb.

THanks for all your help!
--
Nathan
"Richard Cornford" <Ri*****@litote s.demon.co.uk> wrote in message news:<cd******* ************@ne ws.demon.co.uk> ...
Brian wrote:
Nathan Given wrote:

<snip>
Here is the error from the firefox javascript console:
Error: document.getEle mentsByTagName( "BODY")[0] has no
properties.


If you have a JavaScript question, ask in a JavaScript
group. I recommend comp.lang.javas cript.


Fair enough, but the explanation of the error is fairly simple. A script
executed inline within the HEAD section of a page executes prior to the
HTML parser encountering (or implying) the opening BODY tag. As a result
there is no BODY element within the DOM to refer to and -
document.getEle mentsByTagName( "BODY") - must return an empty nodeList at
that point (so there is no item at index zero within that nodeList).

Richard.

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
9105
by: TheKeith | last post by:
I don't know much about javascript, so take it easy on me. Is there a way to dynamically change a CSS layers dimensions on the fly. Here is what I'm doing. I have a bunch of thumbnails that when clicked on, will open up the full view in a CSS layer, as opposed to a pop-up window let's say. I had this before, but with one layer for each image,...
2
10467
by: Markus Mohr | last post by:
Hi, everyone, I have a special problem: For every monitor resolution in 200 pixel steps from 800 to 1600 pixels I have an image to be shown as centered background-image. Those images all have the same name and reside in the following physical path structure:
1
3198
by: Spike | last post by:
Hello! Im going to make a javascript for changing alot of images. But im not sure how to do it., where to start.. Ok, first.. this is the isue. I have 3 images(I call them 1a-3a). when u click on image 1a u change" image x" to image 1a when u click on image 2a u change" image x" to image 2a when u click on image 3a u change" image x" to...
1
2470
by: Martin Nadoll | last post by:
Hello, is it possible to change the bg-Image of a <td>-tag onMouseOver? maybe with javaScript or with css? Thzankis for any help, Martin Nadoll
7
4840
by: Keith Smith | last post by:
How can I change the background IMAGE (not just color) of a CELL in a table? I know that I can do this using CSS, but I really need to be able to do it using JavaScript. Anyone know how? Must be able to change the image for each cell - not the whole table. Please help.
2
22040
by: Mark | last post by:
IE creates an object of every ID'd HTML tag (so it appears), and each object sets a property for any parameter I set in the tag. For example, my HTML might be: <td id='cell1' background='myimg.jpg' width='30'... </tdwhich IE will use to create a Javascript object called 'cell1' with properties of background and width, like so:...
3
3183
by: vj | last post by:
how to change images based on action. Even clicking changed images should do respective actions. and while displaying only one image at a time sholud get displayed. I am using three images for a single column of a table in Jsp. Any clues. Any link for any good javascript html jsp site where i can find some good solution. here swaping...
5
4645
by: studentofknowledge | last post by:
hi guys im new to javascript and need some guidence please.. im trying to display a 400x400 pixel coloured area in the middle of a webpage that stays in the middle with a browser window resize. Within the area, i would like to display an image and some text, overriding a few words with an inline style. i would like the image to change...
2
3804
by: Jim | last post by:
How can I use JavaScript to change a background image in a table cell? Here's the code for the cell <td width="338" valign="top" background="../images/ LEC_Q1.jpg"><div align="right"></div></td> You can see that the background is set to the image LEC_Q1.jpg. I want to change the image LEC_Q1.jpg to LEC_Q1_faded.jpg after a user clicks...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8179
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5700
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5372
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3816
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1431
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.