(先发制人:“为什么我收到此错误:”如果你很想这个标记为重复,注意其他问题似乎问我知道为什么我会出现这个错误;我想知道如何检测我的JavaScript代码中的错误,它只出现在Firebug控制台中,当然,在加载图像时对用户是显而易见的。)检测“图像损坏或截断”在Firefox
I我正在使用picturefill作为响应式图像。我为图像上的加载事件触发了一个回调。因此,每当有人调整浏览器窗口大小时,回调就会运行,以便通过图片填充来加载不同的图像。
回调里面,我的图像数据通过画布转换为dataURL,这样我可以缓存在localStorage的图像数据,以便其提供给即使他们是脱机用户。
备注 “离线” 的部分。这就是为什么我不能依靠浏览器缓存。而HTML5脱机应用程序缓存不符合我的需求,因为图像响应迅速。 (请参阅"Application Cache is a Douchebag"以解释HTML离线应用程序缓存的响应图像的不兼容性。)
在Mac上的Firefox 14.0.1上,如果我调整浏览器的大小然后重新调整大小,则加载图像会触发在大图像有机会完全加载之前再次回到小的位置。它最终会报告Firebug控制台中的“图像损坏或截断”,但不会引发异常或触发错误事件。代码中没有任何迹象表明任何错误。就在Firebug控制台中。同时,它在localStorage中存储截断的图像。
我如何可靠,高效地检测中的JavaScript这个问题,使我不缓存的形象?
这是我遍历picturefill的div如何找到已插入由picturefill IMG标签:
var errorLogger = function() {
window.console.log('Error loading image.');
this.removeEventListener('load', cacheImage, false);
};
for(var i = 0, il = ps.length; i < il; i++){
if(ps[ i ].getAttribute("data-picture") !== null){
image = ps[ i ].getElementsByTagName("img")[0];
if (image) {
if ((imageSrc = image.getAttribute("src")) !== null) {
if (imageSrc.substr(0,5) !== "data:") {
image.addEventListener("load", cacheImage, false);
image.addEventListener('error', errorLogger, false);
}
}
}
}
}
而这里的cacheImage()
回调的样子:
var cacheImage = function() {
var canvas,
ctx,
imageSrc;
imageSrc = this.getAttribute("src");
if ((pf_index.hasOwnProperty('pf_s_' + imageSrc)) ||
(imageSrc.substr(0,5) === "data:") ||
(imageSrc === null) || (imageSrc.length === 0)) {
return;
}
canvas = w.document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
try {
dataUri = canvas.toDataURL();
} catch (e) {
// TODO: Improve error handling here. For now, if canvas.toDataURL()
// throws an exception, don't cache the image and move on.
return;
}
// Do not cache if the resulting cache item will take more than 128Kb.
if (dataUri.length > 131072) {
return;
}
pf_index["pf_s_"+imageSrc] = 1;
try {
localStorage.setItem("pf_s_"+imageSrc, dataUri);
localStorage.setItem("pf_index", JSON.stringify(pf_index));
} catch (e) {
// Caching failed. Remove item from index object so next cached item
// doesn't wrongly indicate this item was successfully cached.
delete pf_index["pf_s_"+imageSrc];
}
};
最后,在这里是我在Firebug我看到全文的URL为保护有罪:
图像损坏或截断:http://www.example.com/pf/external/imgs/extralarge.png
难道不是火''元素上的'error'事件? – MaxArt 2012-08-13 06:18:23
有人可能会问,你为什么试图在本地存储中实现自己的图像缓存,而不是让浏览器缓存执行它的工作? – jfriend00 2012-08-13 06:24:54
@ jfriend00我正在缓存图像以供离线使用。我在这个问题上说了这个,但是我把它看成是在旁边。我已经编辑了这个问题,让离线位更加重视。无论如何,下一个明显的问题是为什么不使用HTML5离线应用程序。有关响应式图片与HTML5脱机应用程序缓存不兼容的解释,请参阅http://www.alistapart.com/articles/application-cache-is-a-douchebag/ – Trott 2012-08-13 14:09:08