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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:20:28+00:00 2026-05-14T02:20:28+00:00

I have a photo gallery web page where a single <img src="XXX" /> element’s

  • 0

I have a photo gallery web page where a single <img src="XXX" /> element’s src is changed (on a click) with JavaScript to show the next image—a poor man’s ajax I guess. Works great on faster connections when the new image appears almost immediately. Even if it takes a few seconds to load, every browser I’ve tested it on keeps the old image in place until the new one is completely loaded.

It’s a little confusing waiting those few seconds on a slow connection, though, and I’m wondering if there’s some JavaScript event that fires when the new image is done loading, allowing me to put a little working… animated gif or something up in the meantime.

I know I could use AJAX for real (I’m using jQuery already), but this is such a nice and simple solution. Besides this lag, is there any other reason I should stay away from this approach to changing images?

  • 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-14T02:20:29+00:00Added an answer on May 14, 2026 at 2:20 am

    You can set up a handler on the “load” event.

    $('img.whatever')
      .load(function() { /* do stuff */ })
      .attr('src', newURL);
    

    Actually I guess you’d want to do this with “live()”:

    $('img.reloadable').live('load', function() { $(this).show(); });
    // ...
    $('img#someId').hide().attr('src', newURL);
    

    edit — whoa, where did that year go? Well, it turns out that one problem with that “live” approach I typed in way back when is that the “load” event does not bubble. Now what you can do, however, is leverage the way that “Image” objects (as opposed to <img> DOM elements) behave. Basically, the function that changes the URL can use an “Image” element as the place to keep the handler. The code that changes the actual “src” attribute of the real <img> tag would then also update the “src” of the “Image” object instance. The browser will only really load the image once (assuming cache control is all cool), but the browser will still call the “onload” handler of the “Image”:

    (function() {
      var imageObj = new Image();
    
      imageObj.onload = function() {
        // code to run when image loads from server 
      };
    
      $('#hypotheticalButton').click(function() {
        $('#imgToUpdate').attr('src', newURL);
        imageObj.src = newURL;
      });
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a friend who is need of a web page. He does interior
I have photo gallery code that does image re-sizing and thumbnail creation. I use
I have a photo gallery I would like that every time a user does
I have a photo gallery I'm trying to set a picture as the cover
I have a photo gallery that has a previous and a Next link. When
I have downloaded a freeware photo gallery called Minishowcase (freeware http://minishowcase.net/?main ) to use
I have a photo website and i want to support tags as my original
I have an application where I need the user to upload a photo. After
I need to create a photo gallery for a website running IIS 4.0 or
I have a problem as the topic name says: Lets say i have gallery

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.