Results 1 to 6 of 6

Thread: Random Image with Javascript

  1. #1
    Otaku Slothrop may be famous one day Slothrop may be famous one day Slothrop's Avatar
    Join Date
    Feb 2005
    Location
    New York.
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random Image with Javascript

    I have the code done already, but it looks extreamly bloated, what I'm asking is what I can do to cut down on the mass of code.

    Code:
    if (document.images)
    	{
    	    pic1= new Image();
    	    pic1.src="image01.jpg";
    	    pic2= new Image();
    	    pic2.src="image02.jpg";
    	    pic3= new Image();
    	    pic3.src="image01.jpg";
    	    pic4= new Image();
    	    pic4.src="image01a.gif";
    	    pic5= new Image();
    	    pic5.src="image01a.gif";
    	    pic6= new Image();
    	    pic6.src="image01a.gif";
    	}
    
    	function get_random(maxNum)
    	{
    	    if (Math.random && Math.round)
    	    {
    	        var ranNum= Math.round(Math.random()*(maxNum-1));
    		ranNum+=1;
    		return ranNum;
    	    }
    	    else
    	    {
    	       today= new Date();
    	       hours= today.getHours();
    	       mins= today.getMinutes();
    	       secn= today.getSeconds();
    	       if (hours==19)
    	       hours=18;
    	       var ranNum= (((hours+1)*(mins+1)*secn)%maxNum)+1;
    	       return ranNum;
    	    }
    	}
    	
    	function get_Image()
    	{
    	    if (document.images)
    	    {
    		var choose_one= get_random(3);
    	   	choose_one--;
    		var pics= new Array(3)
    		pics[0]=pic1.src;
    		pics[1]=pic2.src;
    		pics[2]=pic3.src;
    		pics[3]=pic4.src;
    		pics[4]=pic5.src;
    		pics[5]=pic6.src;
    		document.write("<img src='"+pics[choose_one+3]+"' width=150 height=44><img src='"+pics[choose_one]+"' width=200 height=100>");
    	    }
    	}

  2. #2
    Legendary Otaku Sinistra may be famous one day Sinistra may be famous one day Sinistra's Avatar
    Join Date
    Oct 2004
    Location
    Radford VA
    Posts
    2,528
    Thanks
    0
    Thanked 3 Times in 2 Posts
    use PHP instead of Javascript

  3. #3
    Lost in the ethernet dragonrider2004 may be famous one day dragonrider2004 may be famous one day dragonrider2004's Avatar
    Join Date
    Oct 2004
    Location
    127.0.0.1
    Posts
    729
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i think this is what your looking for......
    Code:
    #!/usr/bin/perl
    $t1="Title Page";
    $path ="wherever";
    $database = "wherever";
    $credit = "whomever";
    $count=0;
    print "Content-type: text/html\n\n";
    
    ####################################################
    ##### Parsing Stuff #
    ####################################################
    read(STDIN, $buffer, $ENV{CONTENT_LENGTH});
    
    @pairs = split(/&/, $buffer, $ENV{'CONTENT_LENGTH'});
    
    foreach $pair (@pairs)
    
    {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
    }
    
    #################################################
    # Print out the contents of $database in a form #
    #################################################
    
    print "<HTML>\n";
    print "<BODY>\n";
    print "
    <body text=#000000 bgcolor=#FFFFFF link=#0000EF vlink=#59188E alink=#FF0000>
    <center><table BORDER COLS=2 WIDTH=100% NOSAVE >
    <tr>
    <td><font color=#000099><center><img src=logocov.gif height=100 width=137></center></font></td>
    
    <br><b><font color=#000099><font size=+2>$t1</font></font></b><p>
    <b><font color=#000099><font size=+0>Click on Thumbnails for full size</font></font></b></td>
    
    </tr>
    
    </table></center>
    
    </body>
    </html>
    <center><TABLE width=100% border=0 cellpadding=1 cellspacing=1>
    ";
    
    ##### Give Credit #####
    
    open (CREDIT, "$credit");
    NEXT:
    while (<CREDIT>) {
    chomp;
    ($c1) = split(/\|/);
    }
    close(CREDIT);
    
    #######################################
    
    open (DATABASE, "$database");
    NEXT:
    while (<DATABASE>) {
    chomp;
    ($p1) = split(/\|/);
    if (($count == 4) or ($count == 8) or ($count == 16) or ($count == 20) or ($count == 24) or ($count == 28) or ($count == 32) or ($count == 36) or ($count == 40) or ($count == 44) or ($count == 48) or ($count == 52) or ($count == 56) or ($count == 60) or ($count == 64)){
    print "<tr></tr>";
    }
    print "<td><center><a href=$path/$p1><img src=$path/$p1 height=150 width=160></a></center><center>$c1</center></td>";
    $count=$count+1
    }
    close(DATABASE);
    
    print "</center></TABLE>\n";
    print "<p>";
    print "<hr>\n";
    print "<CENTER><b><A HREF=http://bermudarace.com>Back to Bermudarace.com</A></b></CENTER>\n";
    print "</BODY>\n";print "</HTML>\n";
    not sure, it's a canned script that i havent played with before.....

    Student: "Umm sensei, the question on the board is wrong."
    Teacher "SHAAAADD UP!"

  4. #4
    Otaku Slothrop may be famous one day Slothrop may be famous one day Slothrop's Avatar
    Join Date
    Feb 2005
    Location
    New York.
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That PERL is straight out, I don't need a sledgehammer to open a walnut.
    Secondly, I don't have a host serving PHP, and even if I did I refuse to use it on something as trivial as making a picture appear randomly. Thanks anyway, though.
    Last edited by Slothrop; May 28, 2005 at 01:55 PM.

  5. #5
    M A S T E R Zui Quan may be famous one day Zui Quan may be famous one day Zui Quan's Avatar
    Join Date
    Oct 2004
    Location
    A Vastly Superior Place
    Posts
    1,726
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Slothrop
    I have the code done already, but it looks extreamly bloated, what I'm asking is what I can do to cut down on the mass of code.

    Code:
    if (document.images)
    	{
    	    pic1= new Image();
    	    pic1.src="image01.jpg";
    	    pic2= new Image();
    	    pic2.src="image02.jpg";
    	    pic3= new Image();
    	    pic3.src="image01.jpg";
    	    pic4= new Image();
    	    pic4.src="image01a.gif";
    	    pic5= new Image();
    	    pic5.src="image01a.gif";
    	    pic6= new Image();
    	    pic6.src="image01a.gif";
    	}
    
    	function get_random(maxNum)
    	{
    	    if (Math.random && Math.round)
    	    {
    	        var ranNum= Math.round(Math.random()*(maxNum-1));
    		ranNum+=1;
    		return ranNum;
    	    }
    	    else
    	    {
    	       today= new Date();
    	       hours= today.getHours();
    	       mins= today.getMinutes();
    	       secn= today.getSeconds();
    	       if (hours==19)
    	       hours=18;
    	       var ranNum= (((hours+1)*(mins+1)*secn)%maxNum)+1;
    	       return ranNum;
    	    }
    	}
    	
    	function get_Image()
    	{
    	    if (document.images)
    	    {
    		var choose_one= get_random(3);
    	   	choose_one--;
    		var pics= new Array(3)
    		pics[0]=pic1.src;
    		pics[1]=pic2.src;
    		pics[2]=pic3.src;
    		pics[3]=pic4.src;
    		pics[4]=pic5.src;
    		pics[5]=pic6.src;
    		document.write("<img src='"+pics[choose_one+3]+"' width=150 height=44><img src='"+pics[choose_one]+"' width=200 height=100>");
    	    }
    	}

    Unfortunately, the code is going to look bloated no matter if you use PERL, PHP, or Javascript. If your code does what you want it to (and it looks like it should), consider yourself lucky and go with it. It should work just fine on your web site. If it's for randomizing a signature, you'll want to try a PHP version as no webmaster with half a brain will allow Javascript to be used in a forum. But of course you already knew that, I was just elaborating for the rest of the peanut gallery thay might read the thread.
    Clan? What Clan?
    I'm the Master. I need no Clan.

  6. #6
    Lost in the ethernet dragonrider2004 may be famous one day dragonrider2004 may be famous one day dragonrider2004's Avatar
    Join Date
    Oct 2004
    Location
    127.0.0.1
    Posts
    729
    Thanks
    0
    Thanked 0 Times in 0 Posts
    also with PHP there is no limitation that the script and the immage be on the same server. you can have the PHP script on one server that allows you a CGI-BIN dir (where the Perl or PHP scripts genreally reside) and the images on a totaly different server(s)

    Student: "Umm sensei, the question on the board is wrong."
    Teacher "SHAAAADD UP!"

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