dual random image genetator...tricky

This is a discussion on "dual random image genetator...tricky" within the JavaScript Forum section. This forum, and the thread "dual random image genetator...tricky are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Aug 24th, 2007, 00:59
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
dual random image genetator...tricky

I need a script that makes a random backround for a single cell. but the tricky part is that i need it to do it for two different cells,,,not load the same image but a similar image. example...if the first cell loads a turtle i need the second cell to load a predetermined image of a another turtls turtle.
Code: Select all
<script type="text/javascript" language="JavaScript">


NumberOfImagesToRotate = 2;


FirstPart = '<img src="images/back';
LastPart = '.jpg">';
index ='t';
 r = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
document.write(FirstPart + r + LastPart);
}
function printtImage() {
document.write(FirstPart + r + index + LastPart);

}
//-->
</script>
and then prints the output to the designated cells. but i need this to make backgrounds instead of placing images.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Sep 1st, 2007, 16:48
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

If these table cells will be the only two, you could simply add ID's to them and modify their background image.. Your script would look like

Code: Select all
 
<script type="text/javascript" language="JavaScript">
 
 
NumberOfImagesToRotate = 2;
 
 
FirstPart = 'url (images/back';
LastPart = '.jpg)';
index ='t';
r = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
tableCell1.style.backgroundImage = FirstPart + r + LastPart;
tableCell2.style.backgroundImage = FirstPart + r + index + LastPart;
}
 
//-->
</script>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Sep 4th, 2007, 00:32
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

This is what i have.....it seems like this would work..i dunno., i dont know javascript though

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<!--[if lt IE 7.]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->
<style type="text/css">
<!--
body {
    background-color: #CCCCCC;
}
-->
</style>
<link href="default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="JavaScript">
 
 
NumberOfImagesToRotate = 2;
 
 
FirstPart = 'url (images/back';
LastPart = '.jpg)';
index ='t';
r = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
tableCell1.style.backgroundImage = FirstPart + r + LastPart;
tableCell2.style.backgroundImage = FirstPart + r + index + LastPart;
}
 
//-->
</script>
</head>

<body class="bg" topmargin="0" marginheight="0">

<table width="800"  align="center"height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="200" id="tableCell1">
<div class="logo"><a href="index.html"><img src="images/logo.png" /></a></div>
    <div id="navs"><a href="repair.html" class="navs">computer repair</a></div>
    <div id="navs"><a href="hosting.html" class="navs">web hosting</a></div>
    <div id="navs"><a href="design.html" class="navs">design</a></div>
    <div id="navs"><a href="repair.html" class="navs">data backup</a></div>
    <div id="navs"><a href="training.html" class="navs">computer training</a></div>
    <div id="navs"><a href="contact.html" class="navs">contact</a></div></div>
    </td>
  </tr>
  <tr>
    <td bgcolor="#CCCCCC">Lorwm aasdasda asd asd asd asd asd asd as das Lorwm aasdasda asd asd asd asd asd asd as das Lorwm  </td>
  </tr>
  <tr>
    <td height="100" id="tableCell2"><div class="footer"><img src="images/copyright.png" /></div></td>
  </tr>
</table>

</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Sep 4th, 2007, 00:39
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

You need to get the ID's though. Try this

Code: Select all
 
<script type="text/javascript" language="JavaScript">
 
NumberOfImagesToRotate = 2;
 
 
FirstPart = 'url (images/back';
LastPart = '.jpg)';
index ='t';
r = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
document.getElementById('tableCell1').style.backgroundImage = FirstPart + r + LastPart;
document.getElementById('tableCell2').style.backgroundImage = FirstPart + r + index + LastPart;
}
 
//-->
</script>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 4th, 2007, 01:26
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

still no go...


updated code

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<!--[if lt IE 7.]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->
<style type="text/css">
<!--
body {
    background-color: #CCCCCC;
}
-->
</style>
<link href="default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="JavaScript">
 
NumberOfImagesToRotate = 2;
 
 
FirstPart = 'url (images/back';
LastPart = '.jpg)';
index ='t';
r = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
document.getElementById('tableCell1').style.backgroundImage = FirstPart + r + LastPart;
document.getElementById('tableCell2').style.backgroundImage = FirstPart + r + index + LastPart;
}
 
//-->
</script>
</head>

<body class="bg" topmargin="0" marginheight="0">

<table width="800"  align="center"height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="200" id="tableCell1">
<div class="logo"><a href="index.html"><img src="images/logo.png" /></a></div>
    <div id="navs"><a href="repair.html" class="navs">computer repair</a></div>
    <div id="navs"><a href="hosting.html" class="navs">web hosting</a></div>
    <div id="navs"><a href="design.html" class="navs">design</a></div>
    <div id="navs"><a href="repair.html" class="navs">data backup</a></div>
    <div id="navs"><a href="training.html" class="navs">computer training</a></div>
    <div id="navs"><a href="contact.html" class="navs">contact</a></div></div>
    </td>
  </tr>
  <tr>
    <td bgcolor="#CCCCCC">Lorwm aasdasda asd asd asd asd asd asd as das Lorwm aasdasda asd asd asd asd asd asd as das Lorwm  </td>
  </tr>
  <tr>
    <td height="100" id="tableCell2"><div class="footer"><img src="images/copyright.png" /></div></td>
  </tr>
</table>

</body>
</html>


are the id's right
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Sep 4th, 2007, 01:41
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

I can't see where you're actually calling the function which would be why nothing is happening.

adding

<script type="text/javascript">
printImage();
</script>

Just after your main table.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Sep 4th, 2007, 01:45
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

i did that and it still wont show.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Sep 4th, 2007, 02:35
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

Hmmm. puzzling. Let's go back to basics and declare all variables and add abit more oomph so we don't clash with reserved words.

Code: Select all
<script type="text/javascript" language="JavaScript">
 
var NumberOfImagesToRotate = 2;
 
 
var FirstPart = 'url (images/back';
var LastPart = '.jpg)';
var alternativeImg ='t';
var randSelection = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
document.getElementById('tableCell1').style.backgroundImage = FirstPart + randSelection + LastPart;
document.getElementById('tableCell2').style.backgroundImage = FirstPart + randSelection + alternativeImage + LastPart;
}
 
//-->
</script>
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Sep 4th, 2007, 02:51
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

still no go


updates html

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<!--[if lt IE 7.]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->
<style type="text/css">
<!--
body {
    background-color: #CCCCCC;
}
-->
</style>
<link href="default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="JavaScript">
 
var NumberOfImagesToRotate = 2;
 
 
var FirstPart = 'url (images/back';
var LastPart = '.jpg)';
var alternativeImg ='t';
var randSelection = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
document.getElementById('tableCell1').style.backgroundImage = FirstPart + randSelection + LastPart;
document.getElementById('tableCell2').style.backgroundImage = FirstPart + randSelection + alternativeImage + LastPart;
}
 
//-->
</script>
</head>

<body class="bg" topmargin="0" marginheight="0">

<table width="800"  align="center"height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="200" id="tableCell1">
<div class="logo"><a href="index.html"><img src="images/logo.png" /></a></div>
    <div id="navs"><a href="repair.html" class="navs">computer repair</a></div>
    <div id="navs"><a href="hosting.html" class="navs">web hosting</a></div>
    <div id="navs"><a href="design.html" class="navs">design</a></div>
    <div id="navs"><a href="repair.html" class="navs">data backup</a></div>
    <div id="navs"><a href="training.html" class="navs">computer training</a></div>
    <div id="navs"><a href="contact.html" class="navs">contact</a></div></div>
    </td>
  </tr>
  <tr>
    <td bgcolor="#CCCCCC">Lorwm aasdasda asd asd asd asd asd asd as das Lorwm aasdasda asd asd asd asd asd asd as das Lorwm  </td>
  </tr>
  <tr>
    <td height="100" id="tableCell2">
<div class="footer"><img src="images/copyright.png" /></div></td>
  </tr>
</table>
<script type="text/javascript">
printImage();
</script>



</body>
</html>

there would be no conflict with css correct.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Sep 4th, 2007, 02:52
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

whe run in ie there is an error "invalid argument".....line 29....but nothing in firefox
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Sep 4th, 2007, 03:05
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

does ff add images correctly?

the only thing i can see that would cause the ie error could be the mat.ceil but it looks fine

I'm not being much help wid this one,, lol
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old Sep 4th, 2007, 03:26
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

i think it was because the variable name did not match in the statement..... "Image" instead of "Img"...i changed it but still no go. I dont know what the problem can be. Can this even be done with an individual cell?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #13  
Old Sep 4th, 2007, 06:45
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

It really should be working - **Rakuli bangs head on keyboad**

My last suggestion would be to fill the table cells with a div

<div style="width: 100%;height:200px" id="tableCell1">
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)

Last edited by Rakuli; Sep 4th, 2007 at 11:32. Reason: Fixed some dodgy code
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #14  
Old Sep 4th, 2007, 20:17
Junior Member
Join Date: Aug 2006
Location: New Jersey
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

no that doesn't do it either. if you say it should be working i trust you....do you have anyone you can ask....should i repost a thread?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #15  
Old Sep 5th, 2007, 05:32
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: dual random image genetator...tricky

Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<!--[if lt IE 7.]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->
<style type="text/css">
<!--
body {
    background-color: #CCCCCC;
}
-->
</style>
<link href="default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="JavaScript">
 
var NumberOfImagesToRotate = 2;
 
 
var FirstPart = 'images/back';
var LastPart = '.jpg';
var alternativeImg ='t';
var randSelection = Math.ceil(Math.random() * NumberOfImagesToRotate);
function printImage() {
//after loading the id's into variables you could almost get rid of the 
//second function
 
tbc1 = document.getElementById('tableCell1');
tbc2 = document.getElementById('tableCell2');
tbc1.style.backgroundImage = 'url('+FirstPart + randSelection + LastPart+')';
tbc2.style.backgroundImage = 'url('+FirstPart + randSelection + alternativeImg + LastPart+')';
}
 
//-->
</script>
</head>
<body class="bg" topmargin="0" marginheight="0">
<table width="800"  align="center"height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="200" id="tableCell1">
<div class="logo"><a href="index.html"><img src="images/logo.png" /></a></div>
    <div id="navs"><a href="repair.html" class="navs">computer repair</a></div>
    <div id="navs"><a href="hosting.html" class="navs">web hosting</a></div>
    <div id="navs"><a href="design.html" class="navs">design</a></div>
    <div id="navs"><a href="repair.html" class="navs">data backup</a></div>
    <div id="navs"><a href="training.html" class="navs">computer training</a></div>
    <div id="navs"><a href="contact.html" class="navs">contact</a></div>
 </div>
    </td>
  </tr>
  <tr>
    <td bgcolor="#CCCCCC">Lorwm aasdasda asd asd asd asd asd asd as das Lorwm aasdasda asd asd asd asd asd asd as das Lorwm  </td>
  </tr>
  <tr>
    <td height="100" id="tableCell2">
<div class="footer"><img src="images/copyright.png" /></div>
</td>
  </tr>
</table>
<script type="text/javascript">
printImage();
</script>
 
</body>
</html>
I just tried this on my computer and it works - the backgroundImage declaration wasn't working quite right.

If you image paths are correct this will work (FF & IE).

Cheers,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #16  
Old Sep 6th, 2007, 01:51
Junior Member