3

我需要一个脚本,它可以根据提供的 HTTP 状态代码确定图像未加载的原因。

我知道图像和对象上的 onError 事件,但它没有传递错误代码。因此,如果图像的来源损坏或发生超时,则以相同的方式处理。

我想要的是有一个可以确定错误代码并采取相应措施的脚本。例如:

  • 404 - 用预定义的图像替换图像
  • 403 - 使用回调函数通知管理员
  • 504 - 尝试重新加载
  • 等等

我在 google 上进行了一些搜索,但除了 onError 事件之外,我还没有找到。

有任何想法吗?

4

1 回答 1

1

the only thing i can think of is to go to a xhr request on fail, with Asset.image from more handling the loading:

new Asset.image('foo.jpg', {
    onload: function() {
        someel.adopt(this);
    },
    onerror: function() {
        new Request({
            url: this.get('src'),
            onComplete: function() {
                console.log(this.status); // 404
            }
        }).get();
    }

});

http://jsfiddle.net/dimitar/2hwej/

not exactly the greatest as it would mean 2 requests. to go around that, you can put your image loading into a xhr request to begin with and then use base64 data to output or something.

于 2012-05-21T09:07:43.873 回答