Javascript Image objects not loaded

Discussion in 'Javascript' started by guywmustang, Sep 2, 2006.

  1. guywmustang

    guywmustang Guest

    So, basically this is the problem.

    I have this...
    <div id="mainframe">
    <img src="image.jpg" id="image">
    </div>

    Then when I try to in a function say...

    function checkImage()
    {
    var image = document.getElementById("image");
    var height = image.height;
    }

    --------------
    Many times the image height will not be defined.

    I recently changed the past so it uses AJAX to change the <img src...
    stuff to return new data for that inner HTML block..

    Still the image will not have properties.
    Now, this is a problem in IE/FF, but not in opera.

    Any ideas on how to make sure the image is loaded so its height/width
    properties are available... either by swapping the image.src or by
    using ajax to swap out the div innerHTML??
     
    guywmustang, Sep 2, 2006
    #1
    1. Advertising

  2. guywmustang

    Randy Webb Guest

    guywmustang said the following on 9/2/2006 3:06 AM:
    > So, basically this is the problem.
    >
    > I have this...
    > <div id="mainframe">
    > <img src="image.jpg" id="image">
    > </div>
    >
    > Then when I try to in a function say...
    >
    > function checkImage()
    > {
    > var image = document.getElementById("image");
    > var height = image.height;
    > }
    >
    > --------------
    > Many times the image height will not be defined.


    It will be defined, it just won't be defined when you try to call it.

    > I recently changed the past so it uses AJAX to change the <img src...
    > stuff to return new data for that inner HTML block..
    > Still the image will not have properties.
    > Now, this is a problem in IE/FF, but not in opera.
    >
    > Any ideas on how to make sure the image is loaded so its height/width
    > properties are available... either by swapping the image.src or by
    > using ajax to swap out the div innerHTML??


    Use the onload of the img to make sure it is loaded.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Sep 2, 2006
    #2
    1. Advertising

  3. guywmustang

    guywmustang Guest

    The onload is exactly what I needed! Thanks!

    I don't know all the javascript events and such, so this is a good
    forum to turn to thanks to the people here!
     
    guywmustang, Sep 4, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcelo Birnbach [MS]
    Replies:
    0
    Views:
    3,365
    Marcelo Birnbach [MS]
    Jun 28, 2003
  2. Udo Corban
    Replies:
    0
    Views:
    3,935
    Udo Corban
    Jan 23, 2004
  3. 7stud
    Replies:
    11
    Views:
    750
    Dennis Lee Bieber
    Mar 20, 2007
  4. Image is not loaded sometimes

    , Sep 4, 2006, in forum: Javascript
    Replies:
    0
    Views:
    96
  5. Replies:
    2
    Views:
    100
Loading...

Share This Page