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 2 of 2
  1. #1
    Registered User
    Join Date
    Jul 2003
    Location
    In the mud.
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Generating a random bg image

    I just want you to know that, yes, I did search before posting. I tried the code I got and it doesn't work. Could someone please tell me what's wrong with it?

    <html>
    <head>
    <script language="JavaScript">
    <!--
    image01= new Image(1024,768)
    image01.src="http://www.mygpop.com/kh1.jpg"
    image02= new Image(1024,768)
    image02.src="http://www.mygpop.com/kh2.jpg"
    image03= new Image(1024,768)
    image03.src="http://www.mygpop.com/ff7cloud.jpg"
    image04= new Image(1024,768)
    image04.src="http://www.mygpop.com/naruto.jpg"
    //-->
    </script>
    </head>

    <script language="JavaScript">
    function rand_int(num_1, num_2)
    {
    return((num_2)?Math.floor(Math.random()
    *(num_2-num_1))+num_1:Math.floor
    (Math.random()*num_1));
    }
    var bgs=["image01.src","image02.src","image03.src","image04.src"]'
    var rand=rand_int(bgs.length);
    document.write('<body background="'+bgs [rand]+'" bgcolor="#FFFFFF" link="#999999" vlink="#FFFFFF" bgproperties="fixed" text="#DDDDDD" onLoad="window.onresize=new Function('if (navigator.appVersion==\'Netscape\') history.go(0);');">');
    </script>

    </html>


    thanks for any help you can give me
    Last edited by MePerson2k; 11-22-2003 at 08:36 PM.

  • #2
    New Coder
    Join Date
    Jul 2003
    Location
    The World Wide Web
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are calling it as a string, and the variable is an object (image)

    Make the variable a string, containing the url. Then use the same code. (and no 'var' word). Oh, I cleaned up ur code a bit, it should work a bit more efficiently now
    PHP Code:
    <script>
    var 
    image=new Array();
    image[0]="http://www.mygpop.com/kh1.jpg";
    image[1]="http://www.mygpop.com/kh2.jpg";
    image[2]="http://www.mygpop.com/ff7cloud.jpg";
    image[3]="http://www.mygpop.com/naruto.jpg";

    var 
    rand=Math.random()*3
    document
    .write("<style>body{");
    switch (
    rand)
    {
    case 
    0:
        
    document.write("background-image:url("+image[0]+")");
      break
    case 
    1:
        
    document.write("background-image:url("+image[1]+")");
      break
    case 
    2:
        
    document.write("background-image:url("+image[2]+")");
      break
    case 
    3:
        
    document.write("background-image:url("+image[3]+")");
      break
    }
    </script> 
    document.write("}</style>");

    That should work.

    Hope this helps

    Cheers, Mat!
    There is always some madness in love. But there is also always some reason in madness....

    [:: World-Craft :-: ::]


  •  

    Posting Permissions

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