类型错误,不是一个函数。

3
我接手了一些JavaScript代码,这段代码曾经有效,但出于某种原因,它现在不再起作用。
我有以下脚本,请注意,我已经删除了部分内容并仅列出了有问题的部分:

function top_frame() {
    top_frame = top.frames[0].document;
    top_frame.write('<HTML><BODY topmargin=0 leftmargin=0 marginheight=0 marginwidth=0><IMG SRC="'+image_url+'/options_top.gif" HEIGHT=25 WIDTH=300></BODY></HTML>')
//  top_frame.write('<HTML><BODY BGCOLOR="#339999"><IMG SRC="images/global/options_top.gif" WIDTH=300 HEIGHT=25 ALT="Product options editor" BORDER="0"></BODY></HTML>')
    top_frame.close();
}
function bottom_frame() {
    bottom_frame = top.frames[2].document;
bottom_frame.write('<HTML><BODY topmargin=0 leftmargin=0 marginheight=0 marginwidth=0><A HREF="javascript:top.update_options()"><IMG SRC="'+image_url+'/options_end.gif" WIDTH=300 HEIGHT=30 ALT="Update and close" BORDER="0"></A></BODY></HTML>')
    bottom_frame.close();
}

</SCRIPT>
<frameset rows="26,*,31" frameborder="NO" border="0" framespacing="0"> 

<frame  src="javascript:top.top_frame()"
            scrolling="NO" marginwidth="0" marginheight="0" frameborder="NO"
            NAME="options_top">

<frame  src="javascript:top.show_start()"
            marginwidth="0" marginheight="0" frameborder="NO" scrolling="auto"
            NAME="options_main">

<frame  src="javascript:top.bottom_frame()" 
            scrolling="NO" marginwidth="0" marginheight="0" frameborder="NO"
            NAME="options_bottom">

</frameset>
</HTML>

我正在页面底部的HTML中调用bottom_frame()show_start()top_frame()函数。然而,在错误选项卡中,我看到以下信息:
TypeError: top.bottom_frame is not a function

我在每个函数调用中都看到这个。有人能建议一下这里的问题是什么吗?


2
在这段编程代码中 function bottom_frame() { bottom_frame = top.frames[2].document; 你将 bottom_frame 从函数重新定义为文档。所以,这段代码第一次可以工作,但之后会失败。 - Grundy
2个回答

4
不要在变量中使用与函数名称相同的名称。
例如,如果您定义了一个名为a的函数,那么不要将其用作变量名。
function a(){a =1};

当执行 a() 时,它将输出1;

但下一次将会出现错误

未捕获的TypeError:a不是一个函数

因为在执行时,它将重新定义 a 为一个 非函数 类型的值,即1。


我已经做了这些更改,但仍然返回相同的错误。 - Liam Fell
@liam,你做了哪些更改? - gurvinder372
我已经修改了变量名,它们现在与函数名不同。 - Liam Fell
@liam,你能分享最新的代码吗?这样我就可以找出它为什么失败了。 - gurvinder372

1

bottom_frame() 在顶级对象上未定义。你已经在全局定义了它,所以要使用 top 调用它。所以不要这样写:

top.bottom_frame()

只需调用。
bottom_frame()

1
“top” 指的是顶部的 “window”,所以在这一侧一切正常。 - Grundy
谢谢,但是执行此操作会返回错误:"bottom_frame()未定义。" - Liam Fell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接