SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Random Images

  1. #1
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random Images

    This may sound simple to some people but I've been trying this for the last hour and I have nothing.

    What I'm trying to do is have a script load a random image but it doesn't work when I add more then one image to the script:
    PHP Code:
    <script language="JavaScript">
      <!--
        function 
    type() {
            var 
    rn Math.round(Math.random()) + 1;
            for (var 
    1<= 4i++) {
                if (
    <= 9) {
                    var 
    "l_0" i;
                } else {
                    var 
    "l_" i;
                }
                
    l.src "i/c_0" rn ".gif";
            }
            
    setTimeout("type()"200);
        }
      -->
    </script> 
    But I can get this to work:
    PHP Code:
    <script language="JavaScript">
      <!--
        function 
    type() {
            var 
    rn Math.round(Math.random()) + 1;
            
    l_01.src "i/c_0" rn ".gif";
            
    setTimeout("type()"200);
        }
      -->
    </script> 
    Any ideas?
    - the lid is off the maple syrup again!

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right - let's get this straight.

    You're trying to set l to be an image object.

    But instead of referencing an object you're declaring it as a value - and then treating it as an object - which may well
    pseudo convert it - but not to any use!

    And then on TOP of that - you're looping from 1 to 4 - but
    building compatiblity for 9 and more - HUH - WHY?!

    I'm SURE you want to really do this:

    var l = document.getElementById('l_0'+i);
    rather than:
    var l = "l_0" + i;

    Since that way you're giving l an OBJECT to whose src you can
    refer, rather than just setting the variable a text value.

    you can see this by doing an alert(l) above trying to set
    the source - since my example will alert "[OBJECT]" and yours
    will alert "l_0*".

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    That's Right. notepad_coder's Avatar
    Join Date
    Apr 2002
    Location
    Colorado
    Posts
    835
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Woohoo you are the best

    This is what I am trying to do just with more images: www.nathantech.com - the little coder guy

    The reason I am using if (i <= 9) is if there is more then 9 l_* then instead of doing l_010 it will do l_10.

    The for (var i = 1; i <= 5; i++) { will change for every page and will be added by PHP so that part doesn't really matter.

    I used this and it worked:
    PHP Code:
    <script language="JavaScript">
      <!--
        function 
    type() {
          var 
    rn Math.round(Math.random()) + 1;
          for (var 
    1<= 5i++) {
            if (
    <= 9) {
              var 
    c_man document.getElementById("l_0" i);
            } else {
              var 
    c_man document.getElementById("l_" i);
            }
            
    c_man.src "i/c_0" rn ".gif";
          }
          
    setTimeout("type()"200);
        }
      -->
    </script> 
    Thanks.
    Last edited by notepad_coder; Jul 15, 2002 at 04:47.
    - the lid is off the maple syrup again!

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's ok mate.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •