捕获脚本错误 - 类型,行和文件
Catch script error - type,line and file
window.onerror = function(type, file, line){
if(type) {
console.log(type);
}
if(file) {
console.log(file);
}
if(line) {
console.log(line);
}
}
当某些.js文件出错时,此代码返回"脚本错误"。我需要错误的类型,文件和行。我怎样才能得到它?当窗口抛出错误时,此脚本可以完美运行,但当.js文件中存在错误时,它就不一样了。我知道我可以在控制台上找到这些东西,但想象一下我没有,也无法安装。
window.onerror = ErrorLog;
function ErrorLog (msg, url, line) {
console.log("error: " + msg + "'n" + "file: " + url + "'n" + "line: " + line);
return true; // avoid to display an error message in the browser
}
Chrome和Firefox中Javascript中报告的神秘"脚本错误"帖子应该可以回答您的"脚本错误"问题。 也就是说,它可能是由"同源政策"引起的。
虽然我仍在寻找为什么 webkit 会给我"未定义"的文件名和"0"行号以用于未捕获的异常。
这是我用来捕获错误的方法。 我让它请求一个 url 指向服务器端脚本的图像。
function logJSErrors(errObj) {
if (!errObj || !errObj.lineNumber || errObj.lineNumber == 0) {
return; // can't use it any way.
}
if (window.location && window.location.toString().indexOf('rfhelper32.js') >= 0) {
return; // ignore the stupid Norton/Firefox conflict
}
var img = new Image();
img.src = "/jserror?m=" + encodeURIComponent(errObj.message) +
"&location=" + encodeURIComponent(window.location) +
"&ln=" + encodeURIComponent(errObj.lineNumber) +
"&url=" + encodeURIComponent(errObj.fileName) +
"&browser=" + encodeURIComponent(errObj.browserInfo);
}
window.onerror = function (msg, url, line) {
logJSErrors({ message : msg,
lineNumber : line,
fileName : url,
browserInfo : window.navigator.userAgent
});
// if a jquery ajax call was running, be sure to make the spinning icons go away
if (jQuery) {
try {
jQuery.event.trigger("ajaxStop");
} catch(e) {/* do nothing */
}
}
};
相关文章:
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- 限制文件类型,大小,单次上传不'无法在jquery文件上传中工作
- 选择特定文件类型时切换额外输入
- 将字符串文件类型/权限转换为数字
- 使用 if - JS 将文件类型附加到 url
- 如果数据的格式略有损坏,是否仍然可以确定其文件类型
- 如何在Joomla模块中通过JavaScript发送输入文件类型
- 如何在Django表单中设置可接受的文件类型'的FileInput小部件
- 根据文件类型取消绑定和重新绑定click事件处理程序
- 禁止的文件类型docx
- 如何将文件类型从Coldfusion代码转换为javascript代码
- 使用Foundation Abide验证文件类型
- Javascript正则表达式文件类型崩溃chrome
- 如何在模式窗口中从html表单文件类型打开图像
- "npm运行构建“;模块解析错误”;您可能需要一个适当的加载程序来处理此文件类型&”;
- jQuery更改时验证文件类型
- Webpack错误:“您可能需要一个适当的加载程序来处理此文件类型”
- 如何使用ReactTestUtils设置HTML5文件类型输入
- Webpack:您可能需要一个适当的加载程序来处理此文件类型
- 文件类型值作为图像源