间歇性 JavaScript 问题

发布于 2024-09-24 12:35:36 字数 355 浏览 4 评论 0原文

我正在通过 eval 运行一些 JavaScript(我知道,开枪吧),它基本上枚举了文档对象上的所有属性。我的问题是,虽然它在 firebug 中工作,但从脚本运行时,它在 Firefox 中抛出未实现的异常。

链接到 JavaScript 脚本、引发的异常以及 firebug 命令的工作。

任何建议这是怎么回事?

根据记录,这是在 Ubuntu 10.04 64 位上的 Firefox 3.6.10 上完成的,而 chrome 没有这个问题。

I'm running some JavaScript via eval (I know, shoot me), that basically enumerates all of the properties on the document object. My issue is that while it works in firebug, it throws a not implemented exception in Firefox, when run from a script.

Link to JavaScript script, the exception thrown, and the firebug command working.

Any suggestions as to what's going on here?

For the record, this is done on Firefox 3.6.10 on Ubuntu 10.04 64-bit, and chrome does not have this issue.

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

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

发布评论

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

评论(1

秉烛思 2024-10-01 12:35:36

错误就在这里:

console.log(result);

删除该行,一切都应该没问题。

console 对象是一个 Firebug 对象(指 Firebug 控制台)。 Safari/Chrome 恰好也实现了一个 console 对象(指 Webkit js 控制台)。 Firefox,实际上其他浏览器没有控制台对象。所以它会抛出一个错误。

顺便说一句:像往常一样,评估是完全没有必要的。这是完全等效的代码:

for (key in document) {
    result[i] = typeof document[key];
    result[i+1]="document."+key;
    i+=2;
}

如果您坚持将其称为 request,则将其用作引用:

var request = window.document;
for (key in request) {
    result[i] = typeof request[key];
    result[i+1]=request+"."+key;
    i+=2;
}

字符串传递对象名称,那么为了理智起见,请以一种不太令人困惑的方式使用 eval:

var string = "window.document";
eval("var request ="+string);
for (key in request) {
    result[i] = typeof request[key];
    result[i+1]=request+"."+key;
    i+=2;
}

如果您坚持通过 我什至不会这样做(有时这是必要的,但只是很少)。

The error is here:

console.log(result);

remove that line and all should be fine.

The console object is a Firebug thing (refers to the Firebug console). Safari/Chrome just happen to also implement a console object (refers to Webkit js console). Firefox, indeed other browsers don't have a console object. So it throws an error.

BTW: As usual, the evals are completely unnecessary. This is exactly equivalent code:

for (key in document) {
    result[i] = typeof document[key];
    result[i+1]="document."+key;
    i+=2;
}

If you insist on calling it request then use it as a reference:

var request = window.document;
for (key in request) {
    result[i] = typeof request[key];
    result[i+1]=request+"."+key;
    i+=2;
}

If you insist on passing object names by string, then for sanity's sake use eval in a less confusing way:

var string = "window.document";
eval("var request ="+string);
for (key in request) {
    result[i] = typeof request[key];
    result[i+1]=request+"."+key;
    i+=2;
}

Though I wouldn't do even that (sometimes it is necessary, but only very rarely).

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