将 javascript 注入到门户应用程序的 iframe 中

发布于 2024-10-08 08:07:18 字数 99 浏览 0 评论 0原文

我有一个框架,里面有一个网络应用程序。它期望某些 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 技术交流群。

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

发布评论

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

评论(2

缪败 2024-10-15 08:07:18

您的问题在细节上有点模糊,例如您是否控制 iframe 内的内容。但是有很多方法可以在框架之间访问/应用 Javascript。

在 iFrame 包含的页面中:

parent.FunctionName();

这将调用包含 iframe 的主页中存在的函数。

类似地:

YourIFrameName.FunctionName();

将从父级调用 iframe 中的函数。

您还可以将所需的 Javascript 函数打包到 .js 文件中。并将它们包含在任何需要它们的页面(iframe 和/或主页)的标题中。

将其包含在您的 中:

<script type="text/javascript" src="YourJavascriptFile.js"></script>

但是,如果您不控制内容并遇到同源策略,您有两个选择:

1)重新考虑您的应用程序。

2)可行的混乱:你需要
从 iframe 调用一个脚本
做了一些 cURL 类型的魔法来拉动
包含的网络应用程序的页面内容,
注入所需的Javascript,然后
输出更改后的内容
有意义的方式。

如果您决定需要走#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:

parent.FunctionName();

This will call a function that exists within your main page that contains the iframe.

Similarly:

YourIFrameName.FunctionName();

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>:

<script type="text/javascript" src="YourJavascriptFile.js"></script>

However, if you do not control the contents, and run into the same origin policy, you have two options:

1) Rethink your application.

2) A workable mess: You would need to
call a script from the iframe that
does some cURL type magic to pull the
page contents of the included web app,
inject the needed Javascript, and then
output the altered contents in a
meaningful way.

If you decided you need to go the route of #2, I can edit with more specifics.

不及他 2024-10-15 08:07:18

正如罗伯特提到的,我认为这违反了大多数(如果不是全部)浏览器的同源策略。

或者,为什么不让 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?

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