質問

I have an HTML5 offline application that works rather well, the cache validates, and it works in general, until I actually force the device (or computer) offline.

I have a handler attached to window.applicationCache.onerror so I can handle and present a prompt for any random errors that may occur:

window.applicationCache.onerror = function (e) {
    console.log(JSON.stringify(e));
    model.errorInfo(JSON.stringify(e));
    model.cacheError(true);
};

However, this error handler also fires when the device is offline, which throws up the error dialog, when it really shouldn't.

The JSON this spits out is as follows:

{"cancelBubble":false,"returnValue":true,
 "srcElement":{"onobsolete":null,"status":1},"defaultPrevented":false,
 "timeStamp":1351875347729,"cancelable":false,"bubbles":false,
 "eventPhase":2,"currentTarget":{"onobsolete":null,"status":1},
 "target":{"onobsolete":null,"status":1},"type":"error"}

I see nothing here too obvious on what I can look for to handle this paticular case. Should I be ignoring errors based on something in here, or doing something else entirely?

役に立ちましたか?

解決

...this error handler also fires when the device is offline, which throws up the error dialog, when it really shouldn't.

This is actually the appropriate event for this situation, as applicationCache is unable to obtain the manifest and therefore act on a possibly updated manifest. It would be nice to have another event for this situation, but right now there isn't one.

If you wanted to display useful information to the user about cache status: Check applicationCache.status every time the error event fires to see if there was an error on updating a previously-cached app (applicationCache.status===1) or on initial caching (applicationCache.status===0).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top