SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Hybrid View

  1. #1
    because you gotta have beer! firegryphon3207's Avatar
    Join Date
    Apr 2000
    Location
    Roxbury/Boston
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why doesn't this script work?

    I'm hoping someone can tell me why this script doesn't work. It's supposed to call up a random photo.
    I get no error messages, but no photo either.
    Code:
    <SCRIPT>  
    <!--  
    var0="http://www.saveourhomes.org/images/boardhelp.JPG"; 
    var1="http://www.saveourhomes.org/images/louise.jpg"; 
    var2="http://www.saveourhomes.org/images/mel.jpg"; 
    var3="http://www.saveourhomes.org/images/mel.jpg";
    now=new Date();  
    num=(now.getSeconds() )%4;  
    if (num == 0)  
    {cliche=var0};  
    if (num == 1)  
    {cliche=var1};  
    if (num == 2)  
    {cliche=var2};  
    if (num == 3)  
    {cliche=var3}; 
     document.write("<img=\"" + cliche + "\">");  
    //-->  
    </SCRIPT>
    I'm sure I'm missing something blatently obvious. I just can't figure out what it is. Thanks in advance.
    Alone we can do so little; together we can do so much.
    -----------------------------------------------------
    tinyplanet.org <--a nifty spot.

  2. #2
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure, but maybe it's because you digits in variable names ?

  3. #3
    because you gotta have beer! firegryphon3207's Avatar
    Join Date
    Apr 2000
    Location
    Roxbury/Boston
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, because I have an identical script for a random quote, and that works like a dream. I didn't even change the variable names. I tried it anyway, and still no errors, no photos.
    Alone we can do so little; together we can do so much.
    -----------------------------------------------------
    tinyplanet.org <--a nifty spot.

  4. #4
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to check what num returns. See if it is either 0,1,2 etc.

  5. #5
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are missing the image src! Also I would not use var a variable name as it is one of javascripts reserved words!
    Code:
    <SCRIPT> 
    <!-- 
    var imageUrls = ["crazy.jpg","crazy1.jpg","crazy2.jpg","crazy3.jpg"];
    var now = new Date(); 
    var num = now.getSeconds(); 
    var cliche = (0 == num) ? imageUrls[0] : (1 == num) ? imageUrls[1] : (2 == num) ? imageUrls[2] : imageUrls[3];
    alert(cliche); 
    document.write('<img src="' + cliche +'">' ); 
    //--> 
    </SCRIPT>

  6. #6
    because you gotta have beer! firegryphon3207's Avatar
    Join Date
    Apr 2000
    Location
    Roxbury/Boston
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!! I knew it had to be something so obscenely obvious!!!

    I'll also incorporate the advice about the var name too even though it seems to have no clear impact. Better safe than sorry
    Alone we can do so little; together we can do so much.
    -----------------------------------------------------
    tinyplanet.org <--a nifty spot.

  7. #7
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though it may not have an effect but it also helps others to understand the code you are writing! And hey its just a good practice to follow the rules when it comes to scripting!

  8. #8
    because you gotta have beer! firegryphon3207's Avatar
    Join Date
    Apr 2000
    Location
    Roxbury/Boston
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Poop_Shoot
    just a good practice to follow the rules when it comes to scripting!
    Which is why I'm changing it. I honestly didn't know that var was a reserved thing when I first wrote it. And I haven't done javascript in soo long. . Thanks again.
    Alone we can do so little; together we can do so much.
    -----------------------------------------------------
    tinyplanet.org <--a nifty spot.

  9. #9
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I hate endless if/elses

    Code:
    <script type="text/javascript">
    <!--
    ar0="http://www.saveourhomes.org/images/boardhelp.JPG"; 
    ar1="http://www.saveourhomes.org/images/louise.jpg"; 
    ar2="http://www.saveourhomes.org/images/mel.jpg"; 
    ar3="http://www.saveourhomes.org/images/mel.jpg";
    now=new Date();  
    num=(now.getSeconds() )%4;  
    cliche = 'ar' + num
     
     document.write("<img src=""\"" + window[cliche] + "\">" );  
    //-->
    </script>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  10. #10
    because you gotta have beer! firegryphon3207's Avatar
    Join Date
    Apr 2000
    Location
    Roxbury/Boston
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using an array would probably be even easier. But I've never been able to get that one off the ground.(read: I can't get it to work in opera.)
    Alone we can do so little; together we can do so much.
    -----------------------------------------------------
    tinyplanet.org <--a nifty spot.

  11. #11
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    var myImgs = new Array();
    myImgs[0]= "http://www.saveourhomes.org/images/boardhelp.JPG";
    ....
    myImgs[3]= "http://www.saveourhomes.org/images/mel.jpg";

    now=new Date();
    num=(now.getSeconds() )%4;
    cliche = myImgs[num];
    document.write('<img src="' + cliche +'">' );

    Note: the first element in an array is '0', not '1' -- so watch your 'for' loops

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  12. #12
    because you gotta have beer! firegryphon3207's Avatar
    Join Date
    Apr 2000
    Location
    Roxbury/Boston
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aaahhh, I see. This is one of those rare times when it's nice to have your little mistakes pointed out to you.
    Alone we can do so little; together we can do so much.
    -----------------------------------------------------
    tinyplanet.org <--a nifty spot.


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
  •