|
#1
|
|||
|
|||
|
I am using a javascript to randomly display pictures on my site, but there has to be two sets.
Basically on one side is the original artwork the client submits, next to it there is some text, then on the right is the artwork created on one of our products. I want the images to change everytime the page is loaded, but the two sets have to coinside. Like, I can't have one piece of artwork on the left, but showing a different piece of artwork created on a product, to the right. If thats at all confusing let me know, here is the code I have been working with. Any help would be appreciated. Code:
<td>
<!-- left -->
<table border="0" cellpadding="0" cellspacing="0" id="table13">
<tr>
<td>
<img src="images/t_11.gif" width="10" height="9" alt="" border="0"></td>
<td background="images/t_13.gif">
<img src="images/t_12.gif" width="6" height="9" alt="" border="0"></td>
<td background="images/t_13.gif" align="right">
<img src="images/t_14.gif" width="6" height="9" alt="" border="0"></td>
<td>
<img src="images/t_15.gif" width="10" height="9" alt="" border="0"></td>
</tr>
<tr valign="top">
<td background="images/t_fon_left.gif">
<img src="images/t_21.gif" width="10" height="6" alt="" border="0"></td>
<td rowspan="2" colspan="2">
<!-- in -->
<table border="0" cellpadding="0" cellspacing="0" width="124" height="124" id="table14">
<tr>
<td>
<p class="center" align="center"> <!--webbot bot="HTMLMarkup" startspan -->
<script language="JavaScript">
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="images/originals/art1a.jpg"
myimages[2]="images/originals/art2a.jpg"
myimages[3]="images/originals/art3a.jpg"
myimages[4]="images/originals/art4a.jpg"
myimages[5]="images/originals/art5a.jpg"
myimages[6]="images/originals/art6a.jpg"
myimages[7]="images/originals/art7a.jpg"
myimages[8]="images/originals/art8a.jpg"
myimages[9]="images/originals/art9a.jpg"
myimages[10]="images/originals/art10a.jpg"
myimages[11]="images/originals/art11a.jpg"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>
<!--webbot bot="HTMLMarkup"
endspan --> </p></td>
</tr>
</table>
<!-- /in -->
</td>
<td background="images/t_fon_right.gif">
<img src="images/t_23.gif" width="10" height="6" alt="" border="0"></td>
</tr>
<tr valign="bottom">
<td background="images/t_fon_left.gif">
<img src="images/t_31.gif" width="10" height="7" alt="" border="0"></td>
<td background="images/t_fon_right.gif">
<img src="images/t_33.gif" width="10" height="7" alt="" border="0"></td>
</tr>
<tr>
<td>
<img src="images/t_41.gif" width="10" height="10" alt="" border="0"></td>
<td background="images/t_fon_bot.gif">
<img src="images/t_42.gif" width="6" height="10" alt="" border="0"></td>
<td background="images/t_fon_bot.gif" align="right">
<img src="images/t_44.gif" width="6" height="10" alt="" border="0"></td>
<td >
<img src="images/t_45.gif" width="10" height="10" alt="" border="0"></td>
</tr>
</table>
<!-- /left -->
</td>
<td><p class="center" align="center">
</p>
<p class="center" align="center"> </p>
<p class="center" align="center">
<font size="4" color="565656">Becomes: </font></p></td>
<td>
<!-- left -->
<table border="0" cellpadding="0" cellspacing="0" id="table17">
<tr>
<td>
<img src="images/t_11.gif" width="10" height="9" alt="" border="0"></td>
<td background="images/t_13.gif">
<img src="images/t_12.gif" width="6" height="9" alt="" border="0"></td>
<td background="images/t_13.gif" align="right">
<img src="images/t_14.gif" width="6" height="9" alt="" border="0"></td>
<td>
<img src="images/t_15.gif" width="10" height="9" alt="" border="0"></td>
</tr>
<tr valign="top">
<td background="images/t_fon_left.gif">
<img src="images/t_21.gif" width="10" height="6" alt="" border="0"></td>
<td rowspan="2" colspan="2">
<!-- in -->
<table border="0" cellpadding="0" cellspacing="0" width="124" height="124" id="table18">
<tr>
<td>
<p class="center" align="center"> <!--webbot bot="HTMLMarkup" startspan -->
<script language="JavaScript">
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="images/wbpssmll.gif"
myimages[2]="images/magbcsmll.gif"
myimages[3]="images/fnb4smll.gif"
myimages[4]="images/basmsmll.gif"
myimages[5]="images/art12a.jpg"
myimages[6]="images/ct4smll.gif"
myimages[7]="images/puzg.gif"
myimages[8]="images/kthrtsmll.gif"
myimages[9]="images/mpsmll.gif"
myimages[10]="images/mugsmll.gif"
myimages[11]="images/ktrdsmll.gif"
//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="../products/wbps.html"
imagelinks[2]="../products/magbc.html"
imagelinks[3]="../products/fnb4.html"
imagelinks[4]="../products/basm.html"
imagelinks[5]="../products/ktrd.html"
imagelinks[6]="../products/ct4.html"
imagelinks[7]="../products/puzg.html"
imagelinks[8]="../products/kthrt.html"
imagelinks[9]="../products/mp.html"
imagelinks[10]="../products/mug.html"
imagelinks[11]="../products/ktrd.html"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>
<!--webbot bot="HTMLMarkup"
endspan --> </p></td></tr></table>
<!-- /in -->
</td>
<td background="images/t_fon_right.gif">
<img src="images/t_23.gif" width="10" height="6" alt="" border="0"></td>
</tr>
<tr valign="bottom">
<td background="images/t_fon_left.gif">
<img src="images/t_31.gif" width="10" height="7" alt="" border="0"></td>
<td background="images/t_fon_right.gif">
<img src="images/t_33.gif" width="10" height="7" alt="" border="0"></td>
</tr>
<tr>
<td>
<img src="images/t_41.gif" width="10" height="10" alt="" border="0"></td>
<td background="images/t_fon_bot.gif">
<img src="images/t_42.gif" width="6" height="10" alt="" border="0"></td>
<td background="images/t_fon_bot.gif" align="right">
<img src="images/t_44.gif" width="6" height="10" alt="" border="0"></td>
<td >
<img src="images/t_45.gif" width="10" height="10" alt="" border="0"></td>
</tr>
</table>
<!-- /left -->
</td>
|
|
#2
|
||||
|
||||
|
Step one - name your files so that the pairs are easily matched up. For instance, leftImg1.jpg would pair up with rightImg1.jpg. The names themselves are not important, just make sure they are consistent and you can calculate them easily.
Next, write a bit of code to pick a random number in your range and store it in a variable. Finally, write the code to set the src attribute of the two images to their names with, like this: document.getElementById('leftImg').src = "leftImg" + myRandomNumber + ".jpg" Also set the other images src attribute using the same variable and the appropriate name. Frank |
|
#3
|
|||
|
|||
|
Quote:
Quote:
Quote:
Code:
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0></a>')
Also the second set has links associated with it so the coding at the end of that one is this: Code:
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
Last edited by SipherDrakon; 07-25-2008 at 12:59 AM. |
|
#4
|
||||
|
||||
|
Quote:
So, instead of this... Code:
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<img src="'+myimages[ry]+'" border=0></a>')
HTML Code:
<img src="" border="0" id="imageA"> <a href="" id="linkB"><img src="" border="0" id="imageB"></a> <script type="text/javascript"> var ry = Math.floor((Math.random()*myimages.length) + 1) ) document.getElementById('imageA').src = 'myimages' + ry + 'a.jpg'; document.getElementById('imageB').src = 'myimages' + ry + 'b.jpg'; document.getElementById('linkB').href = 'mylink' + ry + 'b.html'; </script> Also note that I changed your random number code slightly - the random function gives you a number greater or equal to 0 and less than 1, so when you multiply by x, you may get 0 when you round down but never x. Adding 1 to it means the number you get is between 1 and x, inclusive. Otherwise you get a number between 0 and x-1. You can adjust that to taste by adding or subtracting different ways. Frank |
|
#5
|
|||
|
|||
|
I just can't wrap my head around this. . . this is what i have, but it doesn't do anything. I can't understand it. I did research on it, but i still can't seem to grasp this:
Here is the javascript put between the body tags: Code:
<script language="JavaScript">
var myimages1=new Array()
//specify random images below. You can have as many as you wish
myimages1[1]="images/originals/art1a.jpg"
myimages1[2]="images/originals/art2a.jpg"
myimages1[3]="images/originals/art3a.jpg"
myimages1[4]="images/originals/art4a.jpg"
myimages1[5]="images/originals/art5a.jpg"
myimages1[6]="images/originals/art6a.jpg"
myimages1[7]="images/originals/art7a.jpg"
myimages1[8]="images/originals/art8a.jpg"
myimages1[9]="images/originals/arta9.jpg"
myimages1[10]="images/originals/art10a.jpg"
myimages1[11]="images/originals/art11a.jpg"
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="images/originals/art1b.gif"
myimages[2]="images/originals/art2b.gif"
myimages[3]="images/originals/art3b.gif"
myimages[4]="images/originals/art4b.gif"
myimages[5]="images/originals/art5b.gif"
myimages[6]="images/originals/art6b.gif"
myimages[7]="images/originals/art7b.gif"
myimages[8]="images/originals/art8b.gif"
myimages[9]="images/originals/art9b.gif"
myimages[10]="images/originals/art10b.gif"
myimages[11]="images/originals/art11b.gif"
//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="../products/wbps.html"
imagelinks[2]="../products/magbc.html"
imagelinks[3]="../products/fnb4.html"
imagelinks[4]="../products/basm.html"
imagelinks[5]="../products/ktrd.html"
imagelinks[6]="../products/ct4.html"
imagelinks[7]="../products/puzg.html"
imagelinks[8]="../products/kthrt.html"
imagelinks[9]="../products/mp.html"
imagelinks[10]="../products/mug.html"
imagelinks[11]="../products/ktrd.html"
var ry = Math.floor((Math.random()*myimages.length) + 1) );
document.getElementById('imageA').src = 'myimages1' + ry + 'a.jpg';
document.getElementById('imageB').src = 'myimages' + ry + 'b.gif';
document.getElementById('linkB').href = 'imagelinks' + ry + 'b.html';
</script>
image 1 Code:
<img src="images/originals/art1a.jpg" border="0" id="imageA" alt="Original Artwork"> Code:
<a href="../products/wbps.html" id="linkB"><img src="images/originals/art1b.gif" border="0" id="imageB" alt="Art on Product"></a> Last edited by SipherDrakon; 07-29-2008 at 12:53 AM. |
|
#6
|
|||
|
|||
|
sweet, I got it, I had to change around your coding just a little to get it to work, but thank you for the idea to use getelementbyid.
the + signs and the stuff in quotations just made the function point to that exact wording, which was fine for the images, but the links were where i had to begin to change the coding, also I had to rechange the random function, it gave me an undefined number a few times, so i started the numbers from 0 and took out the +1 on the function, so this is what i came up with and it is working perfectly. Thank you, so much. Code:
<script language="JavaScript">
var images=new Array();
//specify random images below. You can have as many as you wish
images[0]="images/originals/art1a.jpg";
images[1]="images/originals/art2a.jpg";
images[2]="images/originals/art3a.jpg";
images[3]="images/originals/art4a.jpg";
images[4]="images/originals/art5a.jpg";
images[5]="images/originals/art6a.jpg";
images[6]="images/originals/art7a.jpg";
images[7]="images/originals/art8a.jpg";
images[8]="images/originals/art9a.jpg";
images[9]="images/originals/art10a.jpg";
images[10]="images/originals/art11a.jpg";
var myimages=new Array();
//specify random images below. You can have as many as you wish
myimages[0]="images/originals/art1b.gif";
myimages[1]="images/originals/art2b.gif";
myimages[2]="images/originals/art3b.gif";
myimages[3]="images/originals/art4b.gif";
myimages[4]="images/originals/art5b.gif";
myimages[5]="images/originals/art6b.gif";
myimages[6]="images/originals/art7b.gif";
myimages[7]="images/originals/art8b.gif";
myimages[8]="images/originals/art9b.gif";
myimages[9]="images/originals/art10b.gif";
myimages[10]="images/originals/art11b.gif";
//specify corresponding links below
var imagelinks=new Array();
imagelinks[0]="../products/wbps.html";
imagelinks[1]="../products/magbc.html";
imagelinks[2]="../products/fnb4.html";
imagelinks[3]="../products/basm.html";
imagelinks[4]="../products/ktrd.html";
imagelinks[5]="../products/ct4.html";
imagelinks[6]="../products/puzg.html";
imagelinks[7]="../products/kthrt.html";
imagelinks[8]="../products/mp.html";
imagelinks[9]="../products/mug.html";
imagelinks[10]="../products/ktrd.html";
var ry = Math.floor(Math.random()*myimages.length);
function rotatePictures() {
document.getElementById('imageA').src = images[ry];
document.getElementById('imageB').src = myimages[ry];
document.getElementById('linkB').href = imagelinks[ry];
}
onload=rotatePictures;
</script>
Code:
<img src="images/originals/art1a.jpg" border="0" align="center" valign="center" id="imageA" alt="Original Artwork" width="124" height="124"> Code:
<a href="../products/wbps.html" id="linkB"><img src="images/originals/art1b.gif" align="center" valign="center" border="0" id="imageB" alt="Art on Product" width="124" height="124"></a> Last edited by SipherDrakon; 07-29-2008 at 09:52 PM. |
|
#7
|
||||
|
||||
|
Sorry, I tried to post a little more help the other night but I had some connection problems. Glad to see you worked it out.
Frank |
|
#8
|
|||
|
|||
|
Not to butt in on success, but the following may have some advantages:
PHP Code:
1. You can keep the path, filenames, descriptions and links in one array for easier maintanence. 2. It alters the descriptions for the images also. 3. You can have non-sequential filenames that would still be referenced as one, for example: "images/originals/art|thumbnailImage.jpg|fullImage.jpg|Original Artwork 1 - Small|Art on Product 1 - Large|../products/wbps.html", 4. Less code to maintain and mis-type. Note, you could alter so that filenames and 'alt=' descriptions are adjacent if that is important to you (just reset the assignments of the '=tmp[xx]' statements) You can also add a <div> tag and include 'alt' descriptions under or above images displayed. but that would depend upon you design needs. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|