我实际上有同样的问题,并做了一些测试。
我已经做了一些测试iframes并取得了一些成功。经过Firefox 3.6和Chrome测试。对于我使用的9个15Mb的图像。有了img预加载,我多次杀死了我的firefox(是的,在这台计算机上没有太多的记忆),使用img“暂停”动作并不会阻止图像加载,如果请求已经开始,iframes暂停动作真的会停止下载(如我删除iframe)。下一步将用XMLHttpRequests进行测试。由于我的测试代码的这个版本正在使用图片url的braowser缓存行为来防止第二次加载,并且这也适用于ajax请求。但也许与iframe我可以找到一种方法来从iframe中直接加载的图像中检索二进制数据(限制在相同的域名url)。
这是我的测试代码(非常快的浏览器,可太多的非常大的图像杀死你的Firefox):
// jQuery.imageload.shared.list contains an array of oversized images url
jQuery.each(imglist,function(i,imgsrc) {
name = 'imageload-frame';
id = name + "-" + i;
// with img it is not possible to suspend, the get is performed even after remove
//var loader = jQuery('<img />').attr('name', name).attr('id',id);
var loader = jQuery('<iframe />').attr('name', name).attr('id',id);
//no cache on GET query taken from jQuery core
// as we really want to download each image for these tests
var ts = +new Date;
// try replacing _= if it is there
var ret = imgsrc.replace(/(\?|&)_=.*?(&|$)/, "$1_=" + ts + "$2");
// if nothing was replaced, add timestamp to the end
imgsrc = imgsrc + ((ret == imgsrc) ? (imgsrc.match(/\?/) ? "&" : "?") + "_=" + ts : "");
loader.css('display', 'none').appendTo('body');
loader.after(jQuery('<a>')
.text(' stop ')
.attr('href','#')
.click(function(){
loader.remove();
jQuery(this).text(' suspended ');
})
);
// start the load - preload
loader.attr('src',imgsrc);
// when preload is done we provide a way to get img in document
loader.load(function() {
jQuery(this).next("a:first")
.text(" retrieve ").unbind('click')
.click(function() {
var finalimg = jQuery('<img />');
// browser cache should help us now
// but there's maybe a way to get it direclty from the iframe
finalimg.attr('src',loader[0].src);
finalimg.appendTo('body');
});
});
});
编辑,这里是fillde来测试它:http://jsfiddle.net/wPr3x/
警惕't.src =''`,该规范模糊了应该发生的事情。见http://www.nczonline.net/blog/2009/11/30/empty-image-src-can-destroy-your-site/ – 2011-02-07 20:55:39
正确...一些浏览器正在请求网页的网址(或base url)试图用`src =''`加载图像。在Firefox下,设置`t.src = null`也会取消请求,但是对其他人不做任何处理。较新的(当前工作)规范说(在某些情况下)[应该引发错误](http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content-1 .html#update-the-image-data)... kindof。 – Amir 2011-02-07 23:01:43
在Firefox中将`src`设置为嵌入式图像也会取消以前的请求:`t.src ='data:image/gif; base64,R0lGOD ...`在[this fiddle]中看到它(http:// jsfiddle。 net/amirshim/F4HbT/21 /) – Amir 2011-02-07 23:16:47