Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel an image from loading

Suppose in Javascript that you assign the SRC to an IMG tag. It is a large SRC and you want to cancel it before it has finished loading. Assigning the SRC to another image will not stop the data from loading.

That is, in the middle of the load you can assign the SRC to another smaller image and the smaller image will be loaded and appear in your browser. However, the original SRC still continues downloading.

Similarly, deleting the IMG node will not prevent the SRC from continuing to download. No guesses please, look at the repro steps.

REPRO

  1. Load this URL in Chrome in Windows: http://68.178.240.17/imgLoadTest/imgLoadTest.htm

  2. Open up the developer panel by pressing CTRL-SHIFT-J

  3. On the top row of icons in the Chrome developer panel click the Network icon to watch network activity.

  4. On the web page loaded in Step 1 click the Load Image button and watch the developer panel as a large (32meg) image starts loading.

  5. On the web page click the Try To Cancel button to load a different image.

  6. A small image loads, but watch the network in the developer panel and notice that the large image continues to download.

like image 555
eeejay Avatar asked Mar 11 '11 21:03

eeejay


People also ask

Which event is used to cancel the loading of image?

The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image).

How do I remove a source from a picture?

To clear an image src attribute: Use the setAttribute() method to set the image's src attribute to an empty string. Alternatively, hide the image element.

How do you show image only when it is completely loaded?

You can use the complete property to check if the image has finished loading. However, I think there are other issues with your code, mainly you are repeatedly loading the same image. Instead, you should load it only once and then check the complete property in an interval.


3 Answers

Quick answer

Setting the src attribute of the img tag to the empty string will interrupt the current download, even on Chrome.

Details

Nowadays most of browsers implemented that out-of-standard mechanism thought in the old answer to programmatically abort the connection. This is not achieved through a protocol request, but with a client-side in-memory operation. Keep in mind that is not a standard behaviour, but most of vendors courtesy. That is, it could not work on every browser.

I've prepared a jsfiddle showing this mechanism in action (keep an eye at the network panel of the inspector).


Old answer (2011)

Your browser asks for that image with a specific HTTP GET request, as specified in HTTP protocol. Once it asks for it, the http server starts the transfer.

So, it is between the browser (as http client) and the http server.

Since http protocol does not takes into account the option to abort a transfer, the browser should implement a out-of-standard mechanism to programmatically abort the connection. But since this is not specified in any standard, i think you won't find any way to do that with javascript or any client side code.

like image 190
Luca Fagioli Avatar answered Nov 06 '22 15:11

Luca Fagioli


Although I can't find the bug report now, I believe this is a long-standing logged WebKit bug. Firefox (and IE I think) have more sane behavior. I'm going back a bit in my brain, but if I recall on those browsers, resetting the img.src will in fact cancel outstanding downloads. I am positive that Firefox does this when a download is "waiting in line" for a chance at an open HTTP connection, and I seem to recall that it will actually force close connections sometimes.

I've never found a way to coax WebKit based browsers into cancelling an img download in progress, whether it is just queued or already actively downloading.

This really sucks if you're making something like a mapping client and need more fine grained control over images.

like image 31
Stella Laurenzo Avatar answered Nov 06 '22 15:11

Stella Laurenzo


Cancel with transparent base64 encoded GIF to avoid additional requests and double page load on android:

var img = new Image();
img.src = 'http://google.com/favicon.ico';

img.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAI=;'
like image 4
Joshua Robinson Avatar answered Nov 06 '22 16:11

Joshua Robinson