当img.onerror上升时,有没有办法获得有关错误的描述? 我正在使用img.src = .... 我可以捕获错误,但我想了解更多有关错误的信息。
收到的事件不会提供任何信息。
提前致谢。
答案 0 :(得分:0)
一些浏览器不支持onerror。这是普遍捕获堆栈跟踪的最佳方法。
function wrap(func) {
// Ensure we only wrap the function once.
if (!func._wrapped) {
func._wrapped = function () {
try{
func.apply(this, arguments);
} catch(e) {
console.log(e.message, "from", e.stack);
throw e;
}
}
}
return func._wrapped;
};
如果您想继续使用onerror。使用事件监听器。
window.addEventListener("error", function (e) {
console.log(e.error.message, "from", e.error.stack);
});