我有一个页面,其中包含嵌入在对象标记中的另一个页面。 现在我想从“父”页面调用一个 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/