Javascript:从 iframe 调用父级中的函数

发布于 2024-12-01 04:29:18 字数 1063 浏览 1 评论 0原文

您好 StackOverflow 社区,

我的 和 JavaScript 遇到问题。假设我有一个index.html,正文中有一个iframe。在 index.html 中,我将其放在 标记中:

<script type="text/javascript">
    function alertTest() {
        alert("Testing");
    }
</script>

在 iframe 的 src="iframe.htm" 中(位置与index.html 是同一域)。在该 iframe.htm 页面中,我有:

<a href="javascript:void(0);" onclick="window.parent.alertTest();">Click here.</a>

问题是它没有加载警报,并且在 Google Chrome 中返回错误:

“未捕获类型错误:对象 [object DOMWindow] 的属性“alertTest”不是函数

我尝试过的函数 window.parent.alertTest();window.top.alertTest();、top.alertTest();parent.alertTest(); 以及我搜索的类似问题中出现的几乎所有内容

似乎您无法调用 。功能定位在非 iframe 中,从位于所述非 iframe 中的 iframe

请帮忙!

PS 本质上,在实际的最终结果中,我正在尝试调用 Lightbox 打开图像函数(使用 slimbox 因为我能够调用打开图像函数)我也尝试过使用 target="_parent" 属性来实现最终结果。

Hello StackOverflow community,

I was having a problem with my <iframe> and JavaScript. Let's say I have an index.html with an iframe in the body. Within the index.html I have this in the <head> tags:

<script type="text/javascript">
    function alertTest() {
        alert("Testing");
    }
</script>

In the iframe's src="iframe.htm" (The location is the same domain as index.html). Within that iframe.htm page I have:

<a href="javascript:void(0);" onclick="window.parent.alertTest();">Click here.</a>

The problem is it is not loading the alert and in Google Chrome returns the error:

"Uncaught TypeError: Property 'alertTest' of object [object DOMWindow] is not a function

I have tried window.parent.alertTest();, window.top.alertTest();, top.alertTest();, parent.alertTest(); and nearly everything that came up on similar problems I searched.

It seems you are unable to call a function located in a non-iframe, from the iframe located within said non-iframe.

Please help!

P.S. Essentially in the actual final outcome I'm trying to call a Lightbox open image function (Using slimbox because I am able to call the open image function with it from javascript rather than from a href). I have also tried in this final outcome using the target="_parent" property.

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

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

发布评论

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

评论(3

╄→承喏 2024-12-08 04:29:18

尝试像这样定义您的函数:

window.alertTest = function () {
  alert("Testing");
};

...并以与问题中相同的方式调用它;-)

在浏览器中,javascript 中的所有内容都是 window 对象的子级。当您以 function funcName() { ... } 的形式定义函数时,您实际上是在定义 window 对象的私有方法。正常情况下这并不重要,因为所有代码都是在window范围内执行的,所以它可以访问这些私有方法。

但是,当您尝试从外部访问 window 对象(即另一个 window 对象,即您的 iframe 的对象)时,您将无法访问它。如果您以 window.funcName = function () { ... }; 的形式定义函数,您将创建一个特权方法,该方法可以从外部访问,但也可以访问私有方法和属性(即脚本中的变量),因此当您尝试从 iframe 调用它时它将起作用。

如果您想了解有关私有/特权成员及其可访问性的更多信息,请阅读本文

Try defining your function like this:

window.alertTest = function () {
  alert("Testing");
};

...and calling it in the same way you have in the question ;-)

In a browser, everything in javascript is a child of the window object. When you define a function in the form function funcName() { ... } you are effectively defining a private method of the window object. This doesn't matter under normal circumstances, because all code is executed within the scope of window, so it has access to these private methods.

But when you try and access the window object from the outside (i.e. another window object, that of your iframe) you will not be allowed access to it. If you define the function in the form window.funcName = function () { ... }; you are creating a privileged method, that is accessible from the outside but also has access to the private methods and properties (i.e. the variables in the script), so it will work when you try and call it from your iframe.

Read this if you want to know more about private/privileged members and their accessibility.

秋意浓 2024-12-08 04:29:18

使用parent.functionName。看看这个 链接

use parent.functionName. Check out this link

奢欲 2024-12-08 04:29:18

像这样编写你的函数:

alertTest = function(){

  alert("Testing")

}

然后从 iframe 调用它:

parent.alertTest();

Write your function like this:

alertTest = function(){

  alert("Testing")

}

Then call it from iframe:

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