在对象标签中嵌入的html页面中调用javascript函数 [英] Calling a javascript function in html page embedded in an object tag

查看:534
本文介绍了在对象标签中嵌入的html页面中调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中包含另一个嵌入对象标记中的页面. 现在,我想从父"页面调用javascript函数.具体来说:父页面要调用一个函数,该函数位于嵌入式代码内部. 我以前使用过iframe,但是它们在Firefox中引起了一些讨厌的错误,所以我不再想要使用它们.

I have a page which contains another page embedded inside an object tag. Now I want to call a javascript function from the "parent"-page. To be specific: The parent page wants to call a function, which resides inside the embedded code. Former I used iframes but they caused some nasty bugs with Firefox, so I don't want to use them anymore.

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

So, my question is: what would be the best way to achieve this, when using an object tag?

下面是一些示例来说明我想做什么:

Here's some example to illustrate what I want to do:

我在此页面内有一个HTML页面"parent.html",标记内有一些Javascript.这个parent.html也有一个标记,该标记的src是另一个HTML页面,我们将其称为child.html. child.html页面的内容类似于:

I have a HTML page "parent.html" inside this page there is some Javascript inside a tag. This parent.html also has an tag and the src of this tag is another HTML page, let's call it child.html. The child.html page has something like:

这是一些伪代码:

在Child.html中:

in Child.html:

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

在Child.js中:

in Child.js:

function getSomething(){
    return something;
}

在Parent.html中:

in 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调用一个函数. 我该如何实现?

Now: In the Javascript inside the parent.html I need to call a function from the child.js. How can I achieve this?

谢谢:)

推荐答案

使用contentWindow代替contentDocument对我有用:

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

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

这篇关于在对象标签中嵌入的html页面中调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆