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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Object Handling Headache

    Okay, I'm trying to do this random image thing with two images. Here is the jist of everything:

    Code:
    <head>
    <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
    <!--
    var img_header = new Array("header_1.jpg", "header_2.jpg",
    "header_3.jpg", "header_4.jpg");
    
    var img_main= new Array("main_1.jpg", "main_2.jpg",
    "main_3.jpg", "main_4.jpg");
    
    var l = img_header.length;
    
    var rnd_no = Math.round((l-1)*Math.random());
    
    document.header.src = "img_header[rnd_no]";
    document.main.src = "img_main[rnd_no]";
    
    //-->
    </SCRIPT>
    </head>
    <body>
    <img src="header.jpg" name="header">
    <img src="main.jpg" name="main">
    </body>
    I just get the error "'document.header' is null or not an object" and "'document.main' is null or not an object," but they are objects. They're images are they not?

    I need to do this so the random images correspond with each other. Any other ways to do it? This is driving me crazy.

  • #2
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I just took a different route with it and it worked:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    // Set up the image files to be used.
    var theImages = new Array()
    theImages[0] = 'header_1.jpg'
    theImages[1] = 'header_2.jpg'
    
    var theMain = new Array()
    theMain[0] = 'main_1.jpg'
    theMain[1] = 'main_2.jpg'
    
    // do not edit anything below this line
    
    var j = 0
    var p = theImages.length;
    var preBuffer = new Array()
    for (i = 0; i < p; i++){
       preBuffer[i] = new Image()
       preBuffer[i].src = theImages[i]
    }
    var whichImage = Math.round(Math.random()*(p-1));
    function showImage(){
    document.write('<img src="'+theImages[whichImage]+'">');
    }
    function showImage2(){
    document.write('<img src="'+theMain[whichImage]+'">');
    }
    
    //  End -->
    </script>
    Then I called showImage() and showImage2() where I needed them.

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Location
    America
    Posts
    544
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excuse me, Mr Petty Rider, but what exactly do you plan to do with this piece of code:

    Code:
    var j = 0
    ?

    Seems like a mighty lonely fellow, if you ask me.
    LovesWar

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    PettyRider: You can probably use your first script, with a change of a few details:

    document.header --> document.getElementById('header')

    <img src="header.jpg" name="header"> --> <img src="header.jpg" id="header">

    "img_header[rnd_no]"; --> img_header[rnd_no];
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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