Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Need help with random image java code

    hello every body,

    This is my first post so I hope I'm doing it right. I am trying to make 5 pictures randomly change every 2 seconds in java. When I run the html in edit plus it shows the random image and changes at two seconds but when it changes I get an x -out picture. I get the same out come when I run it in Internet Explorer. When I run it in Firefox it dose not change at all. If I refresh it will always give me a random image like its suppose to. My question is am i missing some code?

    Code:
     onload = "var begin = setInterval('changePic()',2000);"
    Should refresh a new picture every 2 secs right. Or is my problem the fact that i am running the program offline? could it be my browsers java version?

    anyway this is what i have:


    Code:
    <HTML>
      
      <HEAD>
     
      <TITLE>random nature pics</TITLE>
    
        <SCRIPT LANGUAGE="JavaScript">
     
    	 <!--
    	document.write("<center><h1>CGS2820 Javascript Random Image</h1></center>");
        document.write("<center><h2>By Seth Johnson</h2></center> <hr />");
     
         //-->
       
       </SCRIPT>
      
    
    		<SCRIPT LANGUAGE="JavaScript">
    			<!--
    				
    				var a = Math.ceil(Math.random()*5);
    					
    					function changePic()
    						{
    							if (a == 1)
    							 {
    							 document.images[0].src = "nature" + a + " .jpg";
    							 a++;
    							 }
    							else if (a == 2)
    							 {
    							 document.images[0].src = "nature" + a + " .jpg";
    							 a++;
    							 }
    							else if (a == 3)
    							 {
    							 document.images[0].src = "nature" + a + " .jpg";
    							 a++;
    							 }
    							else if (a == 4)
    							 {
    							 document.images[0].src = "nature" + a + " .jpg";
    							 a++;
    							 }
    							else 
    							 {
    							 document.images[0].src = "nature" + a + " .jpg";
    							 a=1;
    							 }
    		
    			           }
    
    			//-->
    		
    		</SCRIPT>
    		
      </HEAD>
    
    
    
    
        <body onload = "var begin = setInterval('changePic()',2000);">
    
    		<TABLE>
    			        <TR>
    						<TD></TD>
    						
    						
    						<TD> 
    						
    						<img src = "">
    
    
    						<SCRIPT LANGUAGE="JavaScript">
    						<!--
    				
    							document.images[0].src = "nature" + a + ".jpg";
    							a++;
    
    						//-->
    						</SCRIPT>
    			
    						</TD>
    						
    						
    						<TD></TD>
    			       </TR>
    	   </TABLE>
    	
    	</body>
    
    
    </html>

    Thanks for any help!

  • #2
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    9
    Thanks
    0
    Thanked 4 Times in 4 Posts
    What you have here will work. It looks like your mistake is an extra space when you create the new image sources.

    Code:
    document.images[0].src = "nature" + a + " .jpg";
    Should be:

    Code:
    document.images[0].src = "nature" + a + ".jpg";
    Remove the space before the .jpg.

  • Users who have thanked slickuser for this post:

    eyerate (10-11-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thank you very much It works great now. I knew I missed something small.
    It is nice to have another set of eyes.
    Thanks Again!!!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by eyerate View Post
    I am trying to make 5 pictures randomly change every 2 seconds in java.
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.



    It is your responsibility to die() if necessary….. - PHP Manual

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,857
    Thanks
    78
    Thanked 4,417 Times in 4,382 Posts
    Philip: You forgot to mention that document.write( ) is considered obsolete and bad coding.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by Old Pedant View Post
    Philip: You forgot to mention that document.write( ) is considered obsolete and bad coding.
    document.write() statements must be run before the page finishes loading. document.write() is at best really only useful to write the original content of your page - which is the situation here.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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