javascript - (之后)jQuery获取脚本成功,我看不到(故意)错误

标签 javascript jquery error-handling

我不明白为什么在成功之后我为什么看不到脚本中的错误(我用谷歌搜索,它只是给出了关于jquery get脚本成功/错误时错误处理的很多答案)。

如果我使用“any” js脚本执行此操作

$.getScript('myextrascript.js',function(){console.log('got it!');});

强调“任何” js脚本(我已经由不同的人尝试过至少12个不同的脚本),因为在我的脚本出现了一个错误之后,该错误只是停止了该脚本(我没有控制台错误),我的第一个念头是这样做是否与我的码?

例如说我的额外脚本中有一个故意未声明的变量...

myextrascript.js:
console.log('I can see this in the console no problem');

var declaired='this variable is ok';

undeclaired='this will cause an error';

console.log("I now don't see this console log because my code has stopped");

我通常会在控制台中看到类似的错误
undeclaired is undefined       myextrascript.js:3

但是我看不到额外脚本中的错误,即使我在错误之前加了一行也没有
window.onerror=function(e,script,line){window.console.log("Error: "+e+" ("+script+":"+ line+")");}

除了这些,别无其他想法了,这真令人困惑。

最佳答案

解决了!

"use strict";

window.onerror=function(e,script,line){window.console.log("Error: "+e+" ("+script+":"+ line+")");}

undeclaired=1;

console.log('WINNING!');

我在中添加了“使用严格”;现在它可以正常工作了(坏了,我想怎么办!)
Error: Uncaught ReferenceError: undeclaired is not defined (myextrascript.js:12)
Uncaught ReferenceError: undeclaired is not defined

关于javascript - (之后)jQuery获取脚本成功,我看不到(故意)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20933158/

相关文章:

javascript - 如何使用 knockout.js 将复选框绑定(bind)到 Google map 标记?

javascript - jquery Accordion 激活不起作用

javascript - 使用 jquery 多次对同一个 id 执行点击功能

java - 是否可以响应 http 错误,该代码在 java spring 中是可变的?

c++ - 编译器错误的单元测试

javascript - 打印 HTML 表单

javascript - jQuery 从动态数组中获取值

JQuery 类名选择器以?开头?

c++ - 使用 FormatMessage 检索 Windows API 错误消息时是否应该设置 Arguments 参数?

javascript - 使用 node.js 函数 async.retry 确定成功/失败