问题描述
我有一个博客超过10年&我想在它上面运行一段Javascript来捕获断开的链接。我正在使用:
I have a blog going for over 10 years & I would like to run a piece of Javascript on it that catches broken links. I was using:
function trackError(e) {
var ie = window.event || {};
var errMsg = e.message || ie.errorMessage || "404 error on " + window.location;
var errSrc = (e.filename || ie.errorUrl) + ': ' + (e.lineno || ie.errorLine);
mailme([errMsg, errSrc]);
}
// Triggering an error in the console:
// You have to use something like setTimeout(function() { notThere(); }, 0);
window.addEventListener('error', trackError, true);
但是并没有以有用的方式捕获错误。什么坏了,在哪一行等。
But that's not catching the error in a useful way. What was broken, on which line etc.
错误对象的 JSON.stringify
仅在{isTrusted:true}
中产生,这是无用的。我在Chrome中注意到 e.path
,但在Firefox中没有。有没有办法在Javascript中记录有关损坏图像链接的有用信息,或者我需要在浏览器引擎上提交错误?
JSON.stringify
of the error object results just in "{"isTrusted":true}"
which is useless. I noticed in Chrome there is e.path
, but not in Firefox. Is there a way in Javascript to log useful information about broken image links or I need to file bugs on browser engines?
推荐答案
这是工作。它不会阻止错误在您的控制台中显示在Chrome中,但它正在运行。不要因为您仍然在Chrome中看到错误而感到困扰。您的代码正在执行,您可以编写 mailme
函数,它将执行。我使用以下内容进行测试:
It's working. It will not stop the error from showing in Chrome in your console but it's working. Don't be bothered by the fact that you still SEE the error in Chrome. Your code is executing and you can write your mailme
function and it will execute. I used the following to test:
index.html
<html>
<head>
<script src="./app.js"></script>
</head>
<body>
<img src="http://pictures.natalian.org/screenies/2004/sep/29/13:23:00/">
</body>
</html>
app.js
var mailme = function() {
console.log('Caught!');
}
window.addEventListener('error', function(e) {
var ie = window.event || {};
var errMsg = e.message || ie.errorMessage || "404 error on " + window.location;
var errSrc = (e.filename || ie.errorUrl) + ': ' + (e.lineno || ie.errorLine);
mailme([errMsg, errSrc]);
}, true);
输出(Chrome)
输出(Firefox)
这篇关于捕获网:: ERR_NAME_NOT_RESOLVED用于修复错误的img链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!