如何取消图像加载

发布于 2024-10-21 19:53:49 字数 679 浏览 3 评论 0原文

假设在 Javascript 中您将 SRC 分配给 IMG 标签。这是一个很大的 SRC,您想在它完成加载之前取消它。将 SRC 分配给另一个图像不会阻止数据加载。

也就是说,在加载过程中,您可以将 SRC 分配给另一个较小的图像,该较小的图像将被加载并显示在您的浏览器中。但是,原始 SRC 仍在继续下载。

同样,删除IMG节点也不会阻止SRC继续下载。请不要猜测,请查看重现步骤。

REPRO

  1. 在 Windows 中的 Chrome 中加载此 URL:http://68.178.240.17/imgLoadTest/imgLoadTest.htm

  2. 按 CTRL-SHIFT-J 打开开发者面板

  3. 在 Chrome 开发人员面板的顶行图标上,单击“网络”图标以查看网络活动。

  4. 在第 1 步中加载的网页上,单击“加载图像”按钮,然后观察开发人员面板,开始加载大型 (32meg) 图像。

  5. 在网页上单击“尝试取消”按钮以加载不同的图像。

  6. 加载小图像,但在开发人员面板中观察网络,并注意到大图像继续下载。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

鹿港小镇 2024-10-28 19:53:49

快速解答

img 标记的 src 属性设置为空字符串将中断当前下载,即使在 Chrome 上也是如此。

###细节
如今,大多数浏览器都实现了旧答案中的非标准机制,以编程方式中止连接。这不是通过协议请求来实现的,而是通过客户端内存操作来实现的。请记住,这不是标准行为,而是大多数供应商的礼貌行为。也就是说,它无法在所有浏览器上运行。

我准备了一个 jsfiddle 来展示这个机制的实际应用(密切关注网络


### Old answer (2011)

您的浏览器通过特定的 HTTP GET 请求来请求该图像,如 HTTP 协议中所指定。一旦请求,http 服务器就会开始传输。

因此,它位于浏览器(作为 http 客户端)和 http 服务器之间。

由于 http 协议没有考虑中止传输的选项,因此浏览器应该实现一种不符合标准的机制来以编程方式中止连接。但由于任何标准中都没有指定这一点,我认为您找不到任何方法可以使用 javascript 或任何客户端代码来做到这一点。

Quick answer

Setting the src attribute of the img tag to an 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 take into account the option to abort a transfer, the browser should implement an 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.

夏末的微笑 2024-10-28 19:53:49

使用透明的base64编码的GIF取消以避免额外的请求和android上的双页加载:

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

img.src = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEAAAAALAAAAAABAAEAAAI=;'

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=;'
傻比既视感 2024-10-28 19:53:49

虽然我现在找不到 bug 报告,但我相信这是一个长期记录的 WebKit bug。 Firefox(我认为还有 IE)有更理智的行为。我回想一下,但如果我记得在那些浏览器上,重置 img.src 实际上会取消未完成的下载。我确信 Firefox 在下载“排队”等待打开 HTTP 连接的机会时会执行此操作,而且我似乎记得有时它实际上会强制关闭连接。

我从未找到一种方法来诱使基于 WebKit 的浏览器取消正在进行的 img 下载,无论它只是排队还是已经在积极下载。

如果您正在制作诸如地图客户端之类的东西并且需要对图像进行更细粒度的控制,这真的很糟糕。

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.

回首观望 2024-10-28 19:53:49

将 .src 属性设置为空字符串应该取消加载:

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

......

//cancel load
img.src = '';

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 = '';
吐个泡泡 2024-10-28 19:53:49

是的,当 img 标签具有 src="" 属性时,页面会在 Android 上下载两次。
在最近的 Android 版本中仍然会出现这种情况。
我还没有找到任何其他浏览器可以做到这一点。
但我找到了一个解决方案:使用不带 src 属性的 img 标签。

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.

冷血 2024-10-28 19:53:49

最终的答案是网络工作者。
将图像加载到网络工作线程中,停止网络工作线程将停止图像加载。

你可以从这段代码中得到这个想法:
https://github.com/NathanWalker/ng2-image-lazy-load

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

谁人与我共长歌 2024-10-28 19:53:49

这对我有用:

imageVarName.src = '';
imageVarName.onload = null;
imageVarName.onerror = null;

this work for me:

imageVarName.src = '';
imageVarName.onload = null;
imageVarName.onerror = null;
朱染 2024-10-28 19:53:49

src 属性必须是 有效的非空网址
所以 null 或空字符串是不合法的(尽管它们有时有效)。

使用:(

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

请参阅此处

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)

德意的啸 2024-10-28 19:53:49

遗憾的是,将 src 设置为空字符串在基于 WebKit 的浏览器(如 Safari)中不起作用。这是 Stella 提到的错误报告的链接。

https://bugs.webkit.org/show_bug.cgi?id=6656

Sadly, setting src to an empty string does not work in WebKit-based browsers like Safari. Here is the link to the bug report which Stella mentioned.

https://bugs.webkit.org/show_bug.cgi?id=6656

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文