javascript - 在对象标记中嵌入的 html 页面中调用 javascript 函数

标签 javascript html object-tag

我有一个页面,其中包含嵌入在对象标记中的另一个页面。 现在我想从“父”页面调用一个 javascript 函数。具体来说:父页面想要调用一个函数,该函数驻留在嵌入式代码中。 以前我使用过 iframe,但它们会导致 Firefox 出现一些严重的错误,所以我不想再使用它们了。

编辑: 所以,我的问题是:使用对象标签时,实现此目标的最佳方法是什么?

这里有一些例子来说明我想做什么:

我在该页面内有一个 HTML 页面“parent.html”,标签内有一些 Javascript。这个 parent.html 也有一个标签,这个标签的 src 是另一个 HTML 页面,我们称它为 child.html。 child.html 页面有这样的东西:

这是一些伪代码:

在 Child.html 中:

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

在 Child.js 中:

function getSomething(){
    return something;
}

在 Parent.html 中:

<object id="childObject" data="child.html" width="850" height="510">
</object>

<script>
    // Here I want to access the function from the child.js
    // I tried something like this, but it doesn't work:
    var something = document.getElementById('childObject').contentDocument.getSomething();
</script>

现在: 在 parent.html 的 Javascript 中,我需要从 child.js 调用一个函数。 我怎样才能做到这一点?

谢谢你:)

最佳答案

使用 contentWindow 而不是 contentDocument 对我有用:

<object id="childObject" data="child.html" width="850" height="510">
</object>

<script>
    var something = document.getElementById('childObject').contentWindow.getSomething();
</script>

关于javascript - 在对象标记中嵌入的 html 页面中调用 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30033163/

相关文章:

javascript - 在 Meteor 的服务器上创建用户帐户后,如何自动登录用户?

php - 电子邮件服务器;这种方法对垃圾邮件安全吗?

javascript - js 捆绑和范围困惑

javascript - 如何在页面上向上或向下移动侧边栏?

javascript - 读取 HTML 对象标签中的数据

javascript - 检测对象标签何时加载

javascript - 通过 jquery 从 html 变量中获取特定标签

html - 从子div内部访问父div的宽度

javascript - 使用 JavaScript 触发 CSS 动画淡入淡出 - 仅在一个方向上有效?

html - 如何在 firefox 的 html 页面上显示视频