Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 785289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:51:12+00:00 2026-05-14T20:51:12+00:00

I am having a bit of trouble working out when an image has been

  • 0

I am having a bit of trouble working out when an image has been loaded.

I have been told that the following function will work but it isn’t doing anything.

$("#photos img:first").load(function (){
    alert("Image loaded!");
});

There are no error’s in my code. Everything else in my script works great.

My HTML looks like this.

<div id="photos">
    <img src="../sample1.jpg" style="background-color:#000033" width="1" height="1" alt="Frog!"/>
    <img src="../sample2.jpg" style="background-color:#999999" width="1" height="1" alt="Zooey!"/>
</div>

Do I have the wrong JQuery function? It should also be noted that the visibility is set to hidden. However even when visible there is no alert.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:51:13+00:00Added an answer on May 14, 2026 at 8:51 pm

    The load event of an image is fired when it’s loaded (doh!), and critically, if you don’t hook up your handler before it loads, your handler won’t get called. Browsers will load resources in parallel, so you can’t be sure (even in jQuery’s ready event saying the page’s DOM is ready) that the image hasn’t already been loaded when your code runs.

    You can use the complete property of the image object to know whether it’s already been loaded, so:

    var firstPhoto = $("#photos img:first");
    if (firstPhoto[0].complete) {
        // Already loaded, call the handler directly
        handler();
    }
    else {
        // Not loaded yet, register the handler
        firstPhoto.load(handler);
    }
    function handler() {
        alert("Image loaded!");
    }
    

    There may even be a race condition in that, if the browser in question really implements multi-threaded loading where the image load may occur on a different thread than the Javascript thread.

    Of course, if your selector will match multiple images, you’ll need to handle that; your selector looks like it’s supposed to match only one, so…

    Edit This version allows for multiple images, and I think it handles any non-Javascript race conditions (and of course, at present there are no Javascript race conditions; Javascript itself is single-threaded in browsers [unless you use the new web workers stuff]):

    function onImageReady(selector, handler) {
        var list;
    
        // If given a string, use it as a selector; else use what we're given
        list = typeof selector === 'string' ? $(selector) : selector;
    
        // Hook up each image individually
        list.each(function(index, element) {
            if (element.complete) {
                // Already loaded, fire the handler (asynchronously)
                setTimeout(function() {
                    fireHandler.call(element);
                }, 0); // Won't really be 0, but close
            }
            else {
                // Hook up the handler
                $(element).bind('load', fireHandler);
            }
        });
    
        function fireHandler(event) {
            // Unbind us if we were bound
            $(this).unbind('load', fireHandler);
    
            // Call the handler
            handler.call(this);
        }
    }
    
    // Usage:
    onImageReady("#photos img:first");
    

    A couple of notes:

    • The callback doesn’t get the event object; you could modify it to do so if you liked, but of course, there’d be no event in the case where the image is already loaded, so it would be of limited utility.
    • You could probably use one instead of bind and unbind, but I like the clarity and I’m paranoid. 🙂
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of trouble with a a function that the mongodb c
I'm having a bit of trouble working something out with regards to javascript and
I am having a bit of trouble working out how to use Apache Mina.
I'm having a bit of trouble figuring out something that should be simple. I
I am having trouble working out an xslt transformation that I would really appreciate
I'm currently working with PowerPoint in VB.NET and having a bit of trouble getting
I'm working on an ASP.NET WebForms CMS application, and having a bit of trouble
OK, I'm working on a spellcheck program in C. The bit I'm having trouble
Having a bit of trouble using the List.Find with a custom predicate i have
Having a bit of trouble getting jQuery and Prototype working together. The error console

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.