在单独的框架中执行 Javascript
互联网浏览器 6/7/8。
我使用 eval() 调用来确保代码在多框架网站内的特定框架的上下文中执行。
我遇到的情况是,我基本上将 FrameA 中方法的完全限定名称传递给 FrameB。然后我希望 FrameB 在 FrameA 内执行所述方法 - 但在 FrameA 的上下文中执行。这是至关重要的,因为 IE 内部的限制可能会导致“释放脚本”错误。请参阅:无法从释放的脚本执行代码
FrameB 使用 eval首先获取目标框架对象,然后对结果对象调用 eval 来执行该框架内的方法。因此,请考虑从 FrameB 内部执行以下操作:
eval("top.FrameA").eval("SomeMethod(1,2);");
第一个 eval 返回一个窗口对象,但对第二个 eval 的调用始终会导致“需要对象”错误。
有趣的是,上面的代码在 FrameA 内部运行,但实际上是通过以下代码在 FrameB 内部执行的:
top.FrameB.eval("eval('top.FrameA').eval('SomeMethod(1,2);');");
因此,问题与 eval 语句的嵌套有关。
我在这里缺少什么?
Internet Explorer 6/7/8.
I'm using the eval() calls to ensure that code is executed within the context of particular frames within a multi-frame web site.
The situation I have is that I'm basically passing the fully qualified name of a method in FrameA to FrameB. I then want FrameB to execute said method inside FrameA - but from within the context of FrameA. This is critical due to a limitation inside IE that can result in "freed script" errors otherwise. See: Can't execute code from a freed script
FrameB uses eval to first get the target frame object, and then it calls eval on the resulting object to execute the method that lives within that frame. So consider the following executing from inside FrameB:
eval("top.FrameA").eval("SomeMethod(1,2);");
The first eval returns a window object, but the call to the second eval always results in an "object expected" error.
Interestingly, the above code works from inside FrameA, but it is in fact being executed inside FrameB via the following code:
top.FrameB.eval("eval('top.FrameA').eval('SomeMethod(1,2);');");
So the problem has something to do with the nesting of the eval statements.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解你的问题,我不是 100% 清楚,但请查看此页面上的最后一部分:
http ://www.infimum.dk/HTML/JSwindows.html
我希望它有帮助。
I'm not 100% clear if I understand you question but check out the last section on this page:
http://www.infimum.dk/HTML/JSwindows.html
I hope it helps.