【发布时间】:2016-07-29 01:29:19
【问题描述】:
目前我有两个 HTML 文件。一个名为 index.html,另一个名为 editor.html
在index.html里面我有一个editor.html的iframe 在这个iframe上面也是我做的一个通知系统。要运行通知,它使用我创建的函数,可以像这样使用:
Notification("msg");
当我从 index.html 内部调用它时,这个函数效果很好,因为该函数修改了 index.html 的 html 代码(通过 .innerHTML)。当我尝试从 editor.html 调用它时出现问题
即使在 index.html 的顶部像这样将 editor.html 添加到 index.html 中:
<script src="editor.html"></script>
我在 editor.html 中写了这个:
<script src="index.html"></script>
当我尝试从 editor.html 运行通知函数时出现错误,因为该函数在 index.html 中并修改了 index.html,不是 editor.html。
请记住,每个 index.html 和 editor.html 中的 javascript 都位于标签中,因为文件中还有其他 html。谢谢,如果您需要进一步确认,请提出问题。
【问题讨论】:
-
一个页面永远不能直接在另一个页面上运行代码。他们只能使用
Window.postMessage()进行通信和传输数据。从 iframe 内部,将是window.parent.postMessage(),从 iframe 外部是document.querySelector('iframe').contentWindow.postMessage()。您还需要在任何接收页面上为“消息”事件附加一个事件侦听器。 -
你能举一个使用单独的 index.html 和 editor.html 的例子吗?传递函数和参数的方法?
标签: javascript html tags