How to cancel an image from loading

*爱你&永不变心* 提交于 2019-11-27 22:48:08
Luca Fagioli

Updated

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

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.

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.

Setting the .src property to an empty string should cancel the load:

//load image from url
var img = new Image();
img.src = 'http://somedomain.com/image.jpg';

......

//cancel load
img.src = '';

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=;'

Yes, page is downloaded twice on Android when an img tag has an src="" attribute.
This still occurs on recent Android versions.
I have not found any other browser that does that.
But I found a solution: use an img tag without src attribute.

kofifus

the src property must be a valid non-empty URL So null or empty string are not legal (even though they sometimes work).

USe: img.src='http://xxxx'; img.onerror=null;

(see here)

The ultimative answer is web workers. Load the image inside an webworker and stopping the web worker will stop the image loading.

You can get the idea from this code: https://github.com/NathanWalker/ng2-image-lazy-load

this work for me:

imageVarName.src = '';
imageVarName.onload = null;
imageVarName.onerror = null;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!