查找破坏 IE 的 ECMAScript/JavaScript 错误

发布于 2024-10-04 04:03:39 字数 1162 浏览 0 评论 0原文

我在一个相对庞大的 ECMAScript 代码库(> 60,000 LOC)上工作,我们在检测我们可怕的朋友 Internet Explorer(尤其是 6 和 7)的错误时往往会遇到一些困难。

目前,我已经被一个问题困扰了 3 天,我的 RIA 在 Google Chrome、Firefox 3.6、Opera 和 Internet Explorer 8 中渲染良好,但在 IE7 模式(或使用真正的 IE)运行 Internet Explorer 8 时失败-7)。

我的问题实际上是:如何识别会在 IE7 中产生错误的代码?

通常我依赖 JSLint 并且我倾向于抓住常见的嫌疑人(尾随逗号,我讨厌你),但在这种特殊情况下,我刚刚对我的所有代码(包括源代码和最小化代码)重新运行了 linter,但它并没有给出我通常的怀疑。所以我想我错误地引入了 IE 不喜欢的东西(谁知道,也许我疯了,在某个地方使用了 Array.map 而不是 dojo.map?),它在我脸上爆炸,产生了很好的错误消息,但不根本帮不了我(“[object object]”和“is null”不应该是这样,所以我假设上游有一个错误默默地失败并阻止了该对象的创建)。

我尝试过查看 Google Closure Linter,但它没有产生任何特别的结果,而且我也不认为 Google Closure Compiler 会成为我的救世主。是否有任何工具(命令行、Web 服务或其他)可以像模拟 IE 一样解析/运行代码,以便我们获得适当的错误?

任何提示表示赞赏。

编辑:感谢您迄今为止尝试解决我的问题的帮助,但我真正想问的是是否有工具可以执行此类检查,这意味着针对特定浏览器验证功能集和语法。在我看来,这是 JS 世界中严重缺乏的东西(FF 或 Chrome 则不然,显然它们的调试器更有帮助)。

EDIT2:今天(3天后)我最终找到了问题的根源,通过检查两个分支之间的所有代码更改,并意识到问题实际上已经存在,但以前从未检测到,并返回到更旧的更改以缩小范围混乱并最终在各处添加控制台日志,直到我达到故障点(上帝感谢 emacs 的正则表达式支持,将日志添加到每一行......繁重但有效......)。但有趣的事实是:IE 吞下了一条原本应该在 try catch 块中显示的错误消息,最初捕获了源问题,然后重新抛出它。仍然不知道为什么,但如果没有的话,会更容易找到,因为它就是为了这个目的,以防万一它坏了。诡异的。可能不喜欢深层次的重新抛出。

我将保留这个问题,因为目前实际问题还没有答案。

I work on a relatively huge ECMAScript codebase (> 60,000 LOC) and we tend to struggle a bit with detecting errors for our dreaded friend Internet Explorer (especially 6 and 7).

At the moment, I've been stuck with an issue for 3 days where my RIA renders fine in Google Chrome, Firefox 3.6, Opera and Internet Explorer 8, but fails when running Internet Explorer 8 in IE7-Mode (or with a real IE-7).

My question really is: how do you do to identify code that will produce an error in IE7?

Usually I rely on JSLint and I tend to catch the usual suspects (trailing commas, I loathe you), but in this particular case I have just re-run a linter on all my code, both source and minimized, and it doesn't yield my usual suspects. So I guess I have mistakenly introduced something that IE doesn't like (who knows, maybe I got crazy and used Array.map instead of dojo.map somewhere?) and it blows up in my face, producing nice error messages that don't help me at all ("[object object]" and " is null" where it shouldn't be, so I assume there was an error up-stream that failed silently and prevented this object from being created).

I've tried having a look at the Google Closure Linter but it doesn't yield anything special, and I don't think that the Google Closure Compiler is going to be my savior either. is there any tool (command-line, web-service or other) that can parse/run code as if it were emulating IE so we can get the appropriate errors?

Any tips are appreciated.

EDIT: Thank you for your help in trying to solve my issue so far, but what I am really just asking is if there are tools that do this sort of checks, meaning validating a feature set and syntax against a particular browser. This is the sort of thing severly lacking in the JS-world in my opinion (not so much for FF or Chrome, obviously as their debuggers are a bit more helpful).

EDIT2: I eventually found the root of my problem today (after 3 days) by going through all my code changes between two branches and realizing that the problem was actually already there but never detected before and going back through even older changes to narrow down the mess and eventually end up adding console logs everywhere until I reached a point of failure (God thank emacs for its regular expression support to add logs to every single line... heavy but works...). Fun fact though: IE swallowed an error message supposed to be displayed in the try catch block originally catching the source problem and then re-throwing it. Still don't why, but if it hadn't then that would have been a lot easier to find, as it was intended for that purpose in case it broke. Weird. Might not like deep-levels of re-throw.

I'll leave the question open as for now there are no answer to the actual question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

后知后觉 2024-10-11 04:03:39

您可以尝试使用 IE8 调试器,正如 @galambalazs 所建议的那样; IE6 时代的旧调试器通常没有什么用处。

我一直使用的低级技术是在 Javascript 源代码的大部分周围添加我自己的 try/catch 块,以缩小错误来源的范围。通过迭代调整 try/catch 块,您可以通过源代码进行“二分搜索”,以找到导致异常的代码。您可能会发现,有些代码在 Firefox 中是无害的,但 IE 的解释器会认为它们是错误的。 (公平地说,IE 的严格性通常是合理的,而 Firefox 的宽松行为确实是不可取的。)

因此,换句话说,您将从您怀疑的 Javascript 源开始,或者您可能会对所有包含的 .js 文件执行此操作:

// top of Javascript source file foo.js
try {
  // ... all the code ...
} catch (x) { alert("Error in foo.js: " + x); }

现在,如果您加载页面并收到该警报,那么您就知道错误位于 foo.js 中的某个位置。因此,你把它分成两半:

// top of foo.js
try {
  // ... half the code, roughly ...
}
catch (x) { alert("Error in first half of foo.js: " + x); }
try {
  // ... the rest of the code ...
} catch (x) { alert("Error in second half of foo.js: " + x); }

重复这个过程,你最终会发现问题。

You might try the IE8 debugger, as @galambalazs suggests; the old debugger from the IE6 era was generally not useful.

The low-level technique I've always used is to add my own try/catch blocks around large portions of the my Javascript source to narrow down the source of error. By iteratively adjusting your try/catch blocks you can do a "binary search" through your source to locate the code that's causing an exception. What you'll probably find is that there's some code that's harmless in Firefox but which IE's interpreter considers an error. (To be fair, it's usually the case that IE's strictness is justified, and that the lax behavior of Firefox is really undesirable.)

So in other words, you'd start on a Javascript source that you suspect, or perhaps you'd do this to all of your included .js files:

// top of Javascript source file foo.js
try {
  // ... all the code ...
} catch (x) { alert("Error in foo.js: " + x); }

Now, if you load the page and get that alert, then you know that the error is somewhere in foo.js. Thus, you split it in half:

// top of foo.js
try {
  // ... half the code, roughly ...
}
catch (x) { alert("Error in first half of foo.js: " + x); }
try {
  // ... the rest of the code ...
} catch (x) { alert("Error in second half of foo.js: " + x); }

Repeat that process and you'll eventually find the problem.

笑脸一如从前 2024-10-11 04:03:39

您可以尝试Microsoft 脚本调试器,或Internet Explorer 开发人员工具

有关 IE 8 与旧版本可能不同的完整列表,请参阅:

另请参阅 Web错误跟踪可能出现的怪癖。

最后一点,在每一行上执行 console.log 可能会帮助您以困难的方式找到特定的错误,但考虑到代码量,您实际上应该为模块编写单元测试。这是确保您的应用程序能够在不同的输入和条件下实际运行的唯一方法。

You may try Microsoft Script Debugger, or Internet Explorer Developer Tools.

For complete list of what may be different in IE 8 from the older version consult with:

Also see Web Bug Track for possible quirks.

As a final note, doing console.log on every line may help you to find a particular bug in a hard way, but considering the amount of code you should really write unit test for modules. That's the only way you can make sure that your application will actually run with different inputs and conditions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文