+ Reply to Thread
Results 1 to 6 of 6

Thread: Using Javascript to check if image has loaded?

  1. #1
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Using Javascript to check if image has loaded?

    Hi!

    I guess there's an easy fix for this but I really can't find it. I'm not really into javascript so bear with me

    On the title page of my online game I use javascript to display screenshots.
    I use an invisible fixed ("absolute") positioned div (display: none) as a target for the screenshots.

    When a user clicks on a thumbnail, I call a function called showScreenshot(image).

    This function sets the background of the div to the appropriate screenshot, e.g.
    Code:
    if(image=='admin')
        targetDiv.style.backgroundImage = "url(images/ss_administration.png)";
    I then set the opacity of the Div to 0, turn on the display and call a fade in function.

    Code:
        setOpacity(targetDiv,0);
        targetDiv.style.display = 'block';
        fadeIn('screenshots-target', 0);
    Works nicely. At least when your browser has cached the screenshots. Otherwise the empty div is displayed for a second or so until the image has loaded which occurs everytime you visit the page for the first time.

    Is there a good way to make the fadeIn function wait until the image has loaded? (I guess "onload" is usable somehow? Maybe I should use an img tag inside the div instead of setting the background?

    Of course I could always preload all fullsized screenshots, but I don't want to do that for every visit since not all visitors will look at them.

    If you want to see the code in action, visit www.metropolisonline.org

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Using Javascript to check if image has loaded?

    Quote Originally Posted by Tarzan View Post
    Is there a good way to make the fadeIn function wait until the image has loaded? (I guess "onload" is usable somehow? Maybe I should use an img tag inside the div instead of setting the background?[/URL]
    This is exactly how you do it. Use an image element and register the fade-in function as a "load" event handler on the image element. Whenever you change the source, the handler will be invoked when the new image finishes loading.

    You have to use an image element because only certain elements will fire the load event. Since a background image isn't an element but styling, events don't apply. Generally, the document and replaced elements support load. The DOM event standard only specifies that load fires for documents, frames and object elements, but browsers often support load for other elements.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    gouri78's Avatar
    gouri78 is offline x10Hosting Member gouri78 is an unknown quantity at this point
    Join Date
    Jul 2011
    Location
    India
    Posts
    71

    Re: Using Javascript to check if image has loaded?

    I've got a function that runs after an image is loaded. The only problem is, if the image is loaded already, the .onload doesn't register so the function doesn't run. Is there a way to check if the image is loaded already?

    Code:
    var img = new Image();
    img.src = "http://example.net/picture.jpg";
    img.onload = showImage;

  4. #4
    LostHorizon's Avatar
    LostHorizon is offline x10Hosting Member LostHorizon is an unknown quantity at this point
    Join Date
    Dec 2010
    Posts
    35

    Re: Using Javascript to check if image has loaded?

    Hi "Gouri78",

    Question: How do you know if the function "showImage()" won't run? Is it because that you didn't see the image got displayed?

    Anyway, to answer your question (that is, if I understand it correctly), there is a problem with your codes (in particular, with the order of the execution of your codes). To fix the problem, you'll need to specify the onload event function handler FIRST before you "preload" the image as follows:

    Code:
    var img = new Image();
    img.onload = showImage;
    
    img.src = "http://example.net/picture.jpg";
    and this will fix your problem (the problem with your codes is that if the image is already in the browser's cache, the browser will load it RIGHT AWAY/IMMEDIATELY before the onload event handler is even set, thus resulting in the function never being called/executed).

    ---------- Post added at 09:03 PM ---------- Previous post was at 08:31 PM ----------

    Also, for your information:

    Quote Originally Posted by gouri78 View Post
    Is there a way to check if the image is loaded already?
    If the image "is loaded already" (already in the browser's cache), then the image's dimensions (width and height) would have been AVAILABLE. You can check to see if either one of them is of "number" type (this would mean that the image "is loaded already").

    Hope this helps.
    Website: http://losthorizon.cz.cc/

    "Genius is one percent inspiration and ninety-nine percent perspiration" -- Thomas Alva Edison.

  5. #5
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Re: Using Javascript to check if image has loaded?

    Just a related question, if I use createElement('img') it seems like the img node is created as an open tag (inspected it with FireBug), i.e.,
    Code:
    <img src="target">
    but of course I want a self-closing tag, i.e.,
    Code:
    <img src="target" />
    Is there a way to create a self-closing tag with createElement?

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Using Javascript to check if image has loaded?

    Firebug isn't showing you a source view, it's showing you a DOM tree. The only place there's such a thing as a self-closed tag is in HTML source. Moreover, you don't need to be too concerned with the HTML source that someone else's code generates.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Javascript Image Changer
    By seaside in forum Programming Help
    Replies: 5
    Last Post: 10-16-2009, 03:23 PM
  2. Javascript Check Number Help
    By Chris S in forum Programming Help
    Replies: 2
    Last Post: 03-06-2009, 04:18 PM
  3. [REQ] [1500¢] Javascript Image Slideshow
    By kbjradmin in forum The Marketplace
    Replies: 12
    Last Post: 02-20-2009, 05:42 AM
  4. Email Check JavaScript
    By willemvo in forum Programming Help
    Replies: 2
    Last Post: 09-05-2008, 07:48 AM
  5. Javascript Image Mapping help
    By votter in forum Programming Help
    Replies: 3
    Last Post: 06-20-2008, 09:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers