1

Is there a way to load an image specified in the src attribute of image tag in an html asynchronously?

My ultimate aim is to hit a java class through a image src tag. I want that to be asynchronously without disturbing current functionality of the web page.

How can this be done?

1
  • Whoever voted this question down did so unjustly. There is nothing wrong with asking how to do something that is impossible. Commented Dec 19, 2012 at 1:15

2 Answers 2

2

All images are loaded asychronously. If the <img> tag with src attribute specified is present in the initial HTML of the page, it will start loading immediately as the page's HTML is parsed and loaded.

If you want to control when it starts to load, then you cannot specify the img URL in the HTML of the page.

For example, this javascript will load an img asychronously when you call this function and it will call your callback when it's successfully loaded:

function createImg(url, fn) {
    var img = new Image();
    img.onload = fn;
    img.src = url;
    return(img);
}

You can then put this image tag in your page by adding it to the page DOM if you want.

Based on your comments, it's not clear what you're really trying to do. Perhaps you really just want to issue an ajax call to your server and don't need to mess with img tags at all.

Sign up to request clarification or add additional context in comments.

1 Comment

Instead of using AJAX i was trying with IMG tag, where i was trying IMF src can make a call similar to AJAX in some mean of scripting with out using JS frameworks :) And now I think going with AJAX is safe way.
1

Any IMG tag with a src attribute will load immediately when the page is requested.

You can put the source in a data attribute instead if you want to control how and when the image should be requested.

1 Comment

Yeah IMG tag will load when the page is rendering that i understood but wat i am trying is to use a IMG src attribute to hit a java class by a URL. in that case i dont want the page to wait till the java class do its own functionality king of AJAX function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.