将 javascript 注入到门户应用程序的 iframe 中
我有一个框架,里面有一个网络应用程序。它期望某些 javascript 函数将存在于它可以调用的页面上。如何将这些 javascript 函数从我的父应用程序注入到 iframe 中?
I have an frame that has a web application inside it. it expects that certain javascript functions will exist on the page that it can call. How can I inject these javascript functions into the iframe from my parent application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题在细节上有点模糊,例如您是否控制 iframe 内的内容。但是有很多方法可以在框架之间访问/应用 Javascript。
在 iFrame 包含的页面中:
这将调用包含 iframe 的主页中存在的函数。
类似地:
将从父级调用 iframe 中的函数。
您还可以将所需的 Javascript 函数打包到 .js 文件中。并将它们包含在任何需要它们的页面(iframe 和/或主页)的标题中。
将其包含在您的
中:
但是,如果您不控制内容并遇到同源策略,您有两个选择:
如果您决定需要走#2 的路线,我可以编辑更多细节。
Your question is a little vague on details, such as whether you control the content inside the iframe or not. But there are a number of ways to go about accessing/applying Javascript between frames.
In the page contained within the iFrame:
This will call a function that exists within your main page that contains the iframe.
Similarly:
Will call a function in your iframe from the parent.
You can also package the needed Javascript functions into a .js file. And include them in the header of whatever page needs them (the iframe and/or the main page).
Include this in your
<head>
:However, if you do not control the contents, and run into the same origin policy, you have two options:
If you decided you need to go the route of #2, I can edit with more specifics.
正如罗伯特提到的,我认为这违反了大多数(如果不是全部)浏览器的同源策略。
或者,为什么不让 iframe 内容直接从 iframe 的父级引用它们,而不是尝试将函数注入到 iframe 中?
As Robert mentioned, I think that violates the same origin policy in most (if not all) browsers.
Alternatively, instead of trying to inject the functions into the iframe, why not have the iframe content reference them directly from the iframe's parent?