SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: New image

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    New image

    Hello,

    I am using the "new image" functionality in javascript to have the client load an image. I need a way to tell when the image is loaded, does anyone have a solution to this problem?

    Thanks!

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    MN
    Posts
    178
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I figure I can just do the following to see if it is loaded yet:

    var image = new Image;
    image.src = '...';
    if (image.width > 0) {
    ....


    But, here is the problem

    When I do that it appears that I only get the images width if the image is cached. I have even tried adding a long delay (two seconds... I am on localhost, so that should be plenty) and it seems like it only works when it is cached. Does anyone have an idea of how I can tell when it is loaded so I can get its dimensions (the dimensions is what I need).

  3. #3
    SitePoint Zealot Arkkimaagi's Avatar
    Join Date
    Mar 2002
    Location
    Lappeenranta, Finland
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this help?

    Code:
    var image = new Image;
    image.src = 'http://www.sitepointforums.com/images/sitepoint-logo.gif';
    setTimeout('checkImg()',1000);
    function checkImg(){
      if (image.complete) {	
    	alert("image downloaded\nWidth: "+image.width+"\nHeight: "+image.height);
      }else {
    	alert("Image not yet loaded, waiting 2 seconds..");
    	setTimeout('checkImg()',2000);
      }
    }

  4. #4
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shoundn't

    new Image ;

    be

    new Image();


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
  •