JavaScript 原型可以在父/iframe 上下文之间工作吗?

发布于 2024-10-07 21:59:56 字数 334 浏览 1 评论 0原文

我正在构建一个基于网络的代码编辑器,但在处理焦点时遇到了麻烦。 显然,我需要为每个编辑器实例移动到 iframe 中。 但是,我不确定是否可以使用 JavaScript 的基于原型的 在这种情况下继承。

基本上,我需要包含脚本来包含原型定义和每个 服务于特定 iframe 的对象使用这个单一原型。

从这一点来看,我有两个想法:

  1. 服务 iframe 的对象应该托管在顶部窗口中。
  2. 为 iframe 提供服务的对象应托管在子 iframe 的窗口中。

处理这种情况的最佳方法是什么?我不知道该怎么做 正确处理 JavaScript 上下文边界...

I'm building a web-based code editor and ran into trouble handling focus.
It's obvious that I need to move into iframe for every editor instance.
However, I'm not sure if it's possible to use JavaScript's prototype-based
inheritance in this case.

Basically, I need included script to contain prototype definition and every
object serving specific iframe use this single prototype.

From this point I have two ideas:

  1. Objects serving iframes should be hosted in a top window.
  2. Objects serving iframes should be hosted in child iframe's windows.

What is the best way to handle this situation? I'm not sure how to
correctly deal with JavaScript context boundary...

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

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

发布评论

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

评论(1

执妄 2024-10-14 21:59:56

这是可以做到的。这需要一些工作,并且您可能需要对源进行一些更改。

首先,您必须确保从未在任何代码中使用 window.document。您正在处理多个文档,它们不能共享节点。不过,您可以使用 [node].ownerDocument,这样您就可以进行跨框架安全节点交互。我以前没有使用过 CodePress,但如果它不能以这种方式跨框架工作,我不会感到惊讶。

此外,IE 和对象/函数跨框架也存在一些问题。我记得,有时函数会转换为对象,然后实际上无法调用,检测这一点涉及使用 toString 并将其与“[object Object]”

It can be done. It will take some work and you may have to make some changes to your source.

To start with, you have to make sure you never use window.document in any of the code. You are dealing with multiple documents and they can't share nodes. You can make use of [node].ownerDocument though, that way you will have cross frame safe node interaction. I haven't worked with CodePress before, but I wouldn't be surprised if it can't work across frames in this fashion.

Also, there are some issues with IE and objects/functions cross frame. As I recall, sometimes function get converted to objects and then can't actually be called, and detecting that involves using the toString and comparting it to "[object Object]"

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