HTML Help Forum HTML Help
Please Search for the answer to your question before asking it! Thanks.
 

random background image for a table header (<td>) with
Post a Reply to this Topic Ask a New Question
Click here to go to the original topic
       HTML Help Forum -> Javascript
View previous topic :: View next topic  
Author Message
nash57



Joined: 04 Mar 2006
Posts: 3

Posted: Sat Mar 04, 2006 5:05 pm     random background image for a table header (<td>) with  

Guys, i've been all over the internet trying scripts out but i just cant get my site to work. i wanna have a random image background for my blog's header and still have the title on it... my blog's template's script is:


Code: <html>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=ISO-8859-1'>
<title><%WindowTitle%></title>
<style type='text/css'>



THESE ARE FOR THE HEADER:
td.title {
   height: 230px;
   width: 700px;
   padding: 10px;
   background-image: url(http://i48.photobucket.com/albums/f227/wasaaaaa/1.jpg);
   border: 3px solid #666666;
   border-top: none;
   vertical-align: bottom;

}

 <---! ALL THE OTHER STYLES ARE HERE.... -->



</style>
</head>
<body>

<table width='700' cellpadding='0' cellspacing='0'>
<tr><td class='title' colspan='2'><h1><%Title%> </h1></td></tr>
<tr>
<td class='leftside' width='550' valign='top'>
<!-- BEGIN LEFT SIDE -->


<--! ALL THE OTHER PARTS ARE HERE.... -->


</body>
</html>





and my pictures are at:
http://i48.photobucket.com/albums/f227/wasaaaaa/1.jpg
http://i48.photobucket.com/albums/f227/wasaaaaa/2.jpg
http://i48.photobucket.com/albums/f227/wasaaaaa/3.jpg



(i have five pictures, named 1.jpg, 2.jpg, and so on...)


Help please please! :cry:
chrisxkelley



Joined: 07 Dec 2004
Posts: 246

Posted: Fri Mar 10, 2006 6:36 pm      

are you allowed to use php? asp? javascript? any type of info like that will help
nash57



Joined: 04 Mar 2006
Posts: 3

Posted: Thu Mar 23, 2006 8:27 am     hi! what languages?  

basically, only java and html languages :(
chrisxkelley



Joined: 07 Dec 2004
Posts: 246

Posted: Thu Mar 23, 2006 3:27 pm      

just a simple javascript will work.

<script type="text/javascript">
<!--
function randImg(){

var myImages = Array();
myImages[0] = "someimage.jpeg";
myImages[1] = "someimage.jpeg";
myImages[2] = "someimage.jpeg";

var image = myImages[Math.floor(Math.random()*myImages.length)]

return image;
}
//-->
</script>

then wherever you want to display a random image
<script>
document.write("<img src=\""+randImg()+"\" alt =\"\" />");
</script>


just fill up that array with images and that should work fine
nash57



Joined: 04 Mar 2006
Posts: 3

Posted: Wed Mar 29, 2006 10:55 am     halloo  

actually, i already tried that script...i got it from this forum. the problem is, my script is using some <td> tags....how do i use the same script but retain the <td> aspect? the random images should come out as a background for a cell table... :(
chrisxkelley



Joined: 07 Dec 2004
Posts: 246

Posted: Wed Mar 29, 2006 3:27 pm      

well I didn't get that script from here, I just wrote it... but any way, all you have to do with that script is call the function and it will give you a random image.

so if you want the td to have a background picture


<script>
document.write('<td style="background-image:' + randImg() + ';"></td>');
</script>


wherever you want a random image url to be generated, just stick the content inside <script> tags and use the randImg() function I wrote which will give you a random image.
jriems



Joined: 11 Apr 2006
Posts: 1

Posted: Tue Apr 11, 2006 8:08 am      

chrisxkelley wrote: well I didn't get that script from here, I just wrote it... but any way, all you have to do with that script is call the function and it will give you a random image.

so if you want the td to have a background picture


<script>
document.write('<td style="background-image:' + randImg() + ';"></td>');
</script>


wherever you want a random image url to be generated, just stick the content inside <script> tags and use the randImg() function I wrote which will give you a random image.

I'm trying to get this to work, but I just can't seem to get the background table image to show up. Can you please point me to a page where this script is set up correctly so I can see it in action, and take a look at the source code?

Many thanks!
jriems
jpicot



Joined: 30 May 2006
Posts: 1

Posted: Tue May 30, 2006 10:26 pm      

Try this solution:

head tag Code:
<script language="JavaScript">
var theImages = new Array() // do not change this
theImages[0] = '100.jpg'
theImages[1] = '200.jpg'
theImages[2] = '300.jpg'

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('<TABLE cellpadding="0" cellspacing="0" border="0" width="100%" height="72" background="'+theImages[whichImage]+'"><tr><td>')
}
</script>

in the body:

Code: <script language="JavaScript">
showImage();
</script>

let me know how it works for you...
swami



Joined: 31 May 2006
Posts: 3
Location: Auckland, New Zealand

Posted: Wed May 31, 2006 2:37 am      

Hi Nash57, I'm a one hour old newbie at this forum, but you could try this sequence:

Code: [color=brown]


<html>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=ISO-8859-1'>
<title><%WindowTitle%></title>
<style type='text/css'>

td.title {
   height: 230px;
   width: 700px;
   padding: 10px; 
   border: 3px solid #666666;
   border-top: none;
   vertical-align: bottom;

}

 <---! ALL THE OTHER STYLES ARE HERE.... -->





</style>


<!--   NB:  REMOVE >>       background-image: url(http://i48.photobucket.com/albums/f227/wasaaaaa/1.jpg);    <<  FROM the ABOVE STYLE!!      -->

<!-- AND THIS IS YOUR NEW BIT -->

<script language="JavaScript" type="text/javascript"><!--

var image = new Array();

image[0] = 'http://i48.photobucket.com/albums/f227/wasaaaaa/1.jpg' ;
image[1] = 'http://i48.photobucket.com/albums/f227/wasaaaaa/2.jpg' ;
image[2] = 'http://i48.photobucket.com/albums/f227/wasaaaaa/3.jpg' ;
image[3] = 'http://i48.photobucket.com/albums/f227/wasaaaaa/4.jpg' ;
image[4] = 'http://i48.photobucket.com/albums/f227/wasaaaaa/5.jpg' ;

var index = Math.floor(Math.random() * image.length);
document.write('<style type="text/css"> td.title { background-image: url('+ image[index] +') } </style>');
//-->
</script>

<!-- NEW BIT ENDS -->


</head>
<body>

<table width='700' cellpadding='0' cellspacing='0'>
<tr><td class='title' colspan='2'><h1><%Title%> </h1></td></tr>
<tr>
<td class='leftside' width='550' valign='top'>
<!-- BEGIN LEFT SIDE -->


<--! ALL THE OTHER PARTS ARE HERE.... -->


</body>
</html>


[/color]

I have it working here: http://www.oceania-audio.com/personnel/test.htm

Good luck!

Swami :)


http://www.blueskyzen.com

steinson



Joined: 09 Sep 2006
Posts: 1

Posted: Sat Sep 09, 2006 11:04 am     bg with onclick event  

i used swami's method to write a bg image to a div. it worked great. thanks!

i'd like to make this happen with an onclick event, though.

any suggestions would be appreciated.
 
 
DARFUR
HOSTING / DESIGN
MAKE MONEY

       HTML Help Forum -> Javascript
Page 1 of 1


Powered by phpBB Search Engine Indexer
Powered by phpBB 2.0.19 © 2001, 2002 phpBB Group