Skip to Content
author's profile photo Former Member
Former Member

Random display of images!!

Hi

I'm following the previous post and accordingly i was able to display a image on the iView.

Now I've a list of images

say image1.jpg, image2.jpg, image3.jpg and so on..

Now i want to pick any one of them and display any one of them. how do i go about this.

Following is the code which i wrote can't find the error in it.

<script type="javascript">
var images = new Array();

images[0] = "/images/birds.jpg";
images[1] = "/images/child.jpg";
images[2] = "/images/dolphin1.jpg";
images[3] = "/images/dolphin2.jpg";
images[4] = "/images/fish.jpg";
images[5] = "/images/kayak.jpg";
images[6] = "/images/man.jpg";
images[7] = "/images/pasture1.jpg";
images[8] = "/images/pasture2.jpg";
images[9] = "/images/pasture3.jpg";
images[10] = "/images/runner.jpg";

var rannum = Math.floor(Math.random()*11)
var choice = images[rannum];

</script>

<% String image2 = componentRequest.getWebResourcePath() + "/images/insidetrack3.gif"; %>
<% String image1 = componentRequest.getWebResourcePath() + "/images/pasture1.jpg";%>

<IMG SRC="<%=image1%>" WIDTH="340" HEIGHT="238" BORDER="0" ALIGN="left">
<IMG SRC="<%=image2%>" WIDTH="360" HEIGHT="238" BORDER="0">

can anyone help me out.

Thanks in advance

Srikant

Add a comment
10|10000 characters needed characters exceeded

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 30, 2005 at 09:01 AM

    Hi,

    you do not use the calculated image "choice". You always refer to "/images/insidetrack3.gif"; and "/images/pasture1.jpg";%.

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi Srikant,

      You are still mixing your server and client-side scripting.

      -


      As Ville said, replace:

      <% String image1 = componentRequest.getWebResourcePath() + images(whichImage)%>

      <IMG SRC="<%=image1%>" WIDTH="235" HEIGHT="238" BORDER="0">

      with

      <script language="JavaScript">

      <!--

      document.write('<img src="'theImages[whichImage]'">');

      //-->

      </script>

      -


      Hope this helps.

      Daniel

  • Posted on Oct 05, 2005 at 07:34 AM

    Hi,

    what are you trying to achieve?

    You render static images on the page and some JavaScript

    that holds a collection of URL's to other images.

    From this collection you basically pick 1 with the Math.random() call and store the url in a variable choice. Well... storing a URL string in a variable does usually not make any Image Object visible.

    If you want to apply the image to an existing image on the page you have to do that via JavaScript,

    basically like this:

    <script type="javascript">
    var images = new Array();
     
    images[0] = "/images/birds.jpg";
    images[1] = "/images/child.jpg";
    images[2] = "/images/dolphin1.jpg";
    images[3] = "/images/dolphin2.jpg";
    images[4] = "/images/fish.jpg";
    images[5] = "/images/kayak.jpg";
    images[6] = "/images/man.jpg";
    images[7] = "/images/pasture1.jpg";
    images[8] = "/images/pasture2.jpg";
    images[9] = "/images/pasture3.jpg";
    images[10] = "/images/runner.jpg";
     
    var rannum = Math.floor(Math.random()*11)
    var choice = images[rannum];
    <b>document.getElementById("myimage1").src=choice;
    document.getElementById("myimage2").src=choice;</b> 
    </script>
     
    <% String image2 = componentRequest.getWebResourcePath() + "/images/insidetrack3.gif"; %>
    <% String image1 = componentRequest.getWebResourcePath() + "/images/pasture1.jpg";%>
     
    <IMG <b>id=myimage1</b> SRC="<%=image1%>" WIDTH="340" HEIGHT="238" BORDER="0" ALIGN="left">
    <IMG <b>id=myimage2</b> SRC="<%=image2%>" WIDTH="360" HEIGHT="238" BORDER="0">

    If you like to do the random part on the server side you should also have you Array of URL's and the random call on the server side.

    Best Regards

    Martin

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.