Detecting "image loaded successfully"

Discussion in 'JavaScript' started by SlimShady, Dec 21, 2006.

  1. #1
    I have a page with content.
    Upon a certain action, two images get updated/replaced.
    Because the images are on another server, it sometimes takes a couple of seconds.
    * I would like to block specific actions (click on a button/other image) UNTIL the images are loaded.
    * How do I detect when the images are done loading?
    Something like readyState, complete, or something?
    Any advice?

    Thanks in advance.
     
    SlimShady, Dec 21, 2006 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    link to original page would be useful....
     
    krakjoe, Dec 21, 2006 IP
  3. SlimShady

    SlimShady Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No, can't do.
    It's a beta-site, only for invited people.

    Let's say we have:
    <img id='orimg' src='http://www.currentsite.com/img/item01.jpg' />
    HTML:
    and we change it using JavaScript:
    <script type="text/javascript">
       function changePic() {
          var imgobj = document.getElementById('orimg');
          imgobj.src = 'http://www.othersite.com/img/item01.jpg';
       }
    </script>
    HTML:
    Is there a property I can check, to check the state (loading, loaded, error) ?
     
    SlimShady, Dec 21, 2006 IP
  4. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #4
    I'm not sure (this is only an idea): maybe testing for image height or witdh, you'll get undefined or real values. ???
     
    ajsa52, Dec 21, 2006 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    <script type="text/javascript">
       function changePic() {
          var imgobj = document.getElementById('orimg');
          imgobj.src = 'http://www.othersite.com/img/item01.jpg';
         
          imgobj.onload = function()
          {
                alert('Do whatever');
          }
       }
    </script>
    
    Code (javascript):
     
    nico_swd, Dec 22, 2006 IP