JavaScript:错误“需要对象”。 我无法破译它。 你可以吗?
我在我的一个网站中使用了一种名为“Facelift 1.2”的 JavaScript,虽然该脚本可以在 Safari 3、4b 和 Opera、OmniWeb 和 Firefox 中运行,但在任何 IE 版本中都不起作用。 但即使在工作浏览器中,我也会收到以下错误,我无法破译。
也许在适当的时候——在 Javascript 方面有了更多的经验——我就能做到,但现在我想我应该问你们中的一些人,在 SO。
以下是我在 IETester 测试 Internet Explorer 6,7 和 8 页面时收到的错误弹出窗口:
以下内容来自 Firefox 3.0.6 中的 Firebug 控制台:
网站是:http://www.457cc.co.nz/index.php 如果它可以帮助您查看实际中提到的问题。
我还查了第 620 行对应的内容是: “第 76 行”是:
this.isCraptastic = (typeof document.body.style.maxHeight=='undefined');
这是该代码块的一部分(取自 flir.js):
// either (options Object, fstyle FLIRStyle Object) or (fstyle FLIRStyle Object)
,init: function(options, fstyle) { // or options for flir style
if(this.isFStyle(options)) { // (fstyle FLIRStyle Object)
this.defaultStyle = options;
}else { // [options Object, fstyle FLIRStyle Object]
if(typeof options != 'undefined')
this.loadOptions(options);
if(typeof fstyle == 'undefined') {
this.defaultStyle = new FLIRStyle();
}else {
if(this.isFStyle(fstyle))
this.defaultStyle = fstyle;
else
this.defaultStyle = new FLIRStyle(fstyle);
}
}
this.calcDPI();
if(this.options.findEmbededFonts)
this.discoverEmbededFonts();
this.isIE = (navigator.userAgent.toLowerCase().indexOf('msie')>-1 && navigator.userAgent.toLowerCase().indexOf('opera')<0);
this.isCraptastic = (typeof document.body.style.maxHeight=='undefined');
if(this.isIE) {
this.flirIERepObj = [];
this.flirIEHovEls = [];
this.flirIEHovStyles = [];
}
}
整个脚本也可以在我的服务器上使用: http://www.457cc.co.nz/facelift-1.2/flir.js
我只是不知道知道从哪里开始寻找错误,特别是因为它只影响 IE,但适用于其他浏览器。 也许你们有一个想法。 我很想听听他们的声音。
谢谢阅读。 Jannis
PS:这是 Opera 的错误控制台报告的内容:
JavaScript - http://www.457cc.co.nz/index.php
Inline script thread
Error:
name: TypeError
message: Statement on line 620: Cannot convert undefined or null to Object
Backtrace:
Line 620 of linked script http://www.457cc.co.nz/facelift-1.2/flir.js
document.body.appendChild(test);
Line 70 of linked script http://www.457cc.co.nz/facelift-1.2/flir.js
this.calcDPI();
Line 2 of inline#1 script in http://www.457cc.co.nz/index.php
FLIR.init();
stacktrace: n/a; see 'opera:config#UserPrefs|Exceptions Have Stacktrace'
I am using a javascript called 'Facelift 1.2' in one of my websites and while the script works in Safari 3, 4b and Opera, OmniWeb and Firefox it does not in any IE version.
But even in the working browser i get the following error I cannot decipher.
Maybe in due time—with more experience in things Javascript—I will be able to but for now I thought I would ask some of you, here at SO.
The following is the error popup i get in IETester testing the page for Interet Explorer 6,7 and 8:
The following is from the Firebug console in Firefox 3.0.6:
The website is: http://www.457cc.co.nz/index.php In case it helps you see the problem mentioned in action.
I have also looked up what line 620 corresponds to which is:
"line 76" is:
this.isCraptastic = (typeof document.body.style.maxHeight=='undefined');
which is part of this block of code (taken from the flir.js):
// either (options Object, fstyle FLIRStyle Object) or (fstyle FLIRStyle Object)
,init: function(options, fstyle) { // or options for flir style
if(this.isFStyle(options)) { // (fstyle FLIRStyle Object)
this.defaultStyle = options;
}else { // [options Object, fstyle FLIRStyle Object]
if(typeof options != 'undefined')
this.loadOptions(options);
if(typeof fstyle == 'undefined') {
this.defaultStyle = new FLIRStyle();
}else {
if(this.isFStyle(fstyle))
this.defaultStyle = fstyle;
else
this.defaultStyle = new FLIRStyle(fstyle);
}
}
this.calcDPI();
if(this.options.findEmbededFonts)
this.discoverEmbededFonts();
this.isIE = (navigator.userAgent.toLowerCase().indexOf('msie')>-1 && navigator.userAgent.toLowerCase().indexOf('opera')<0);
this.isCraptastic = (typeof document.body.style.maxHeight=='undefined');
if(this.isIE) {
this.flirIERepObj = [];
this.flirIEHovEls = [];
this.flirIEHovStyles = [];
}
}
The whole script is also available on my server: http://www.457cc.co.nz/facelift-1.2/flir.js
I just don't know where to start looking for the error, especially since it only affects IE but works in the rest. Maybe you guys have an idea. I would love to hear them.
Thanks for reading.
Jannis
PS: This is what Opera's error console reports:
JavaScript - http://www.457cc.co.nz/index.php
Inline script thread
Error:
name: TypeError
message: Statement on line 620: Cannot convert undefined or null to Object
Backtrace:
Line 620 of linked script http://www.457cc.co.nz/facelift-1.2/flir.js
document.body.appendChild(test);
Line 70 of linked script http://www.457cc.co.nz/facelift-1.2/flir.js
this.calcDPI();
Line 2 of inline#1 script in http://www.457cc.co.nz/index.php
FLIR.init();
stacktrace: n/a; see 'opera:config#UserPrefs|Exceptions Have Stacktrace'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我同意 tvanfosson 的观点 - 您收到该错误的原因很可能是因为您在页面加载完成之前调用
init()
,因此document.body
是尚未定义。在您链接的页面中,您应该将以下代码移动到页面底部(就在结束 html 标记之前:
更好的是,您应该将初始化附加到文档的
ready
事件中。如果您这样做这样,甚至不需要将 javascript 移动到文件底部使用 jquery:有关 jquery 的 document.ready 事件的更多信息 »
I agree with tvanfosson - the reason you're getting that error is quite likely because you're calling
init()
before the page is done loading, sodocument.body
is not yet defined.In the page you linked, you should move the following code to the bottom of the page (just before the closing html tag:
Even better, you should attach the initialization to the document's
ready
event. If you do it this way, there is no need to even move your javascript to the bottom of the file. Using jquery:More on jquery's document.ready event »
编辑 留下答案供上下文参考。 请参阅@Triptych(已接受)的答案以获得正确的解决方案。
我的建议是将 javascript 的包含内容移至标记的末尾。 我认为发生的情况是代码在 DOM 完全加载之前执行,因此当您尝试引用它来确定 maxHeight 样式属性时, document.body 为 null 。 将 javascript 包含到标记的末尾应该足以保证至少加载文档正文并避免此特定错误。
Edit Answer left for context. See @Triptych's (accepted) answer for the correct resolution.
My suggestion is to move the inclusion of the javascript to the end of your mark up. I think what is happening is that the code is executing before the DOM is completely loaded and thus the document.body is null when you try to reference it in determining the maxHeight style property. Moving the inclusion of the javascript to the end of your markup should be enough to guarantee that the body of the document is loaded at least and avoid this particular error.
安装.net Framework v2并解决问题。
Install .net Framework v2 and solve the problem.