简体   繁体   English

如何捕获客户端丢失的资源?

[英]How to capture client-side missing resources?

I'm trying to capture instances of missing resources by adding the following code snippet to my site's <head> :我试图通过将以下代码片段添加到我的网站的<head>来捕获丢失资源的实例:

window.onerror = function(message, file, line) {
    console.log('Error:', message, file, line);
}

But this only seems to capture JS errors, not general DOM errors.但这似乎只捕获 JS 错误,而不是一般的 DOM 错误。 I've tried deliberately: 1) creating <script> tags pointing to non-existent files, and 2) creating <img> tags pointing to non-existent images.我故意尝试过:1)创建指向不存在文件的<script>标签,以及 2)创建指向不存在图像的<img>标签。 Neither triggers an error event.两者都不会触发error事件。

My questions are:我的问题是:

  1. What sorts of errors is onerror listening for? onerror监听什么样的错误?
  2. How do I capture these errors?如何捕获这些错误? Why isn't the above code working?为什么上面的代码不起作用?

如果您认为某个特定元素可能具有不存在的图像或脚本,并且您通常不需要捕获内容,则可以将onerror添加到这些特定的imgscript标签:

<img src="http://something/that/might/not/exist.png" onerror="alert('Error!');">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM