如何在文件中保存JavaScript错误 [英] How to save the JavaScript errors in file

查看:170
本文介绍了如何在文件中保存JavaScript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一个解决方案来保存JavaScript错误的日志。用于自动测试任何支持流行的网络浏览器(IE,FF,Chrome)的网站。

Need a solution for saving log of JavaScript errors. For automated testing any site with support popular web browsers (IE, FF, Chrome).

推荐答案

最实用的方法是寻找恐怖事件。 try-catch是最好的方法,但您应该知道代码中可能出现错误的位置。

Most practical method is to look for an event onerror. try-catch is the best method, but you should know where in your code is possible to appear an error.

此处使用警报。它可以替换为对某些服务器端脚本/应用程序的ajax调用,该脚本/应用程序将负责消息的数据库日志记录。 JavaScript本身无法访问任何文件系统 - 服务器或用户。这只会发送有关错误的信息。 演示

Here alert is used. It can be replaced with an ajax call to some server-side script/app which will be responsible for the database logging of the message. JavaScript by itself can`t access any file-system - server's or user's. This will only send info about the error. demo

window.onerror = function (msg, url, num) {
    alert("Error: " + msg + "\nURL: " + url + "\nLine: " + num);
    return true;
};
JS-made-poo();

Internet Explorer使用ActiveX,这在某些日志记录应用程序中可能很有用。但是当ActiveX被触发时,用户可能会收到警告。

Internet Explorer uses ActiveX, which may be useful in some kind of a logging application. But the user probably will receive a warning when the ActiveX is fired.

这篇关于如何在文件中保存JavaScript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆