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 6676689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:01:25+00:00 2026-05-26T04:01:25+00:00

I wanted to know how to use JQuery to create a function that will

  • 0

I wanted to know how to use JQuery to create a function that will run on each time an image has been finished loading. I want that function to be called EACH TIME, not only once. So if, for example, I have “image1.jpg” and it was loaded on page start, the function will be called. Then, if I change the image and it was loaded again after I changed it using Javascript, I want that ‘loaded’ function to fire again. I tried Jquery Load() function, but it only run once, not the second time after the image has been replaced with another one.

I want also that the even will happen even if the image has been cached too. That means that every time I change thew ‘src’ attribute of the image, I want that a specific function to fire up – again, even if the image is already been cached.

Example:

$("#myimageselector").attr("src", "http://domain.com/the_new_image.jpg");

I want that when this happens, even if the image is already cached the function will fire. I think that it’s possible to know if the images has been cached, kind of. I just need to have an .load() function that will run once (of course), and if it hasn’t been executed, it means that the image has been cached. At least for my specific usage.

Thought: maybe we should bind an event that checked a change in the DOM for a specific element change. So maybe if the image has been finished loading, a specific DOM element is changed, so we can check the before and after values.

Thanks.

  • 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-26T04:01:26+00:00Added an answer on May 26, 2026 at 4:01 am

    The closest I get to your desired solution, to also deal with cached images.
    I’ve created a jQuery plugin:

    (function($){
        var dummy_img = "dummy1x1.png",
            dummy_width = 1,
            dummy_height = 1;
    
        $("<img>").attr("src", dummy_img); //Preload image
    
        $.fn.setSrc = function(src, func, args){
            var $img = this;
            if(/img/i.test(this.get(0).tagName)) return; //Cancel at non-IMG elements
            if(!(args instanceof Array)) args = [];
            var poller = window.setInterval(function(){
                if($img.height() != dummy_height || $img.width() != dummy_width){
                    loaded();
                }
            }, 200);
            $img.attr("src", dummy_img);
            $img.bind("load", loaded);
            $img.bind("error", clearPoller);
            function loaded(){
                clearPoller();
                //If a callback function exists, execute it
                if(typeof func == "function") func.apply($img,args);
    
                func = null;//Prevent unwanted executions
            }
            function clearPoller(){
                window.clearInterval(poller);
            }
        }
    })(jQuery);
    

    Usage:

    $("#myImg").setSrc("image.png", function(){
        alert(this.attr("src") + " has loaded!");
    });
    

    The concept behind this:

    1. Change width and height to 1 (using the preloaded dummy image)
    2. Bind load and error event handlers
    3. Run a poller which checks for the width and height of the image.
    4. When the image has finished/failed loading, the load or error event will be triggered. Upon triggering, the poller is cleared, and the passed function is executed.
    5. When none of these events are fired, the poller is still able to detect a change in image size. When the cached image is loaded, the width/height of the element updates, which is detected by the poller. In this case, the poller clears the interval, and executes the passed function.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We know that we can use a concept Java Package, but I just wanted
I wanted to know how to use threads in Cocoa. I'm new to this
I wanted to know, while deciding which language or technology to use for implementing
Thanks for looking at this question. I wanted to know how can I use
I use jquery to post to an MVC controller action that returns a table
I'm using jquery's .ajax() method on a button click. I wanted to know if
I wanted know how the kernel is providing memory for simple C program .
Wanted to know if someone had a suggestion on code or maybe there's a
I wanted to know what the public opinion is about Resharper vs Devxpress CodeRush
I wanted to know why UDP is used in RTP rather than TCP ?.

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.