在IE中嵌套iframe中访问javascript函数 [英] Accessing a javascript function in a nested iframe in IE

查看:89
本文介绍了在IE中嵌套iframe中访问javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个页面结构,其中包含一个包含iframe(iframe0)的页面(Parent),在iframe内部我有另一个iframe(iframe1)。在iframe1中我有一个javascript函数,我试图从Parent调用。在Firefox / Chrome / Safari中,我可以使用以下代码调用此函数:

  frames [iframe0] [ iframe1\" ]使用functionName()。 

但是,在Internet Explorer中,上面的代码不起作用,它返回错误对象没有支持此属性或方法。我已经尝试了一些其他方法来访问该方法,它们都返回相同的错误。

  window.frames.iframe0 [iframe1] .functionName(); 
window.iframe0.iframe1.functionName();
window.frames.iframe0.frames.iframe1.functionName();

我甚至尝试在iframe0中调用一个函数来调用iframe1中的函数,甚至没有工作。



任何人都知道如何访问嵌套在2级深度的iframe中的javascript函数?



谢谢。



更新:
进一步研究问题后,我发现我遇到的问题与我提出的问题无关。答案ylebre在下面回答了我问过的问题,并且我将标记为答案。我可能会开始另一个更详细地描述我的问题的问题。

解决方案

我提供了一个使用3个HTML文件的示例。最外层是test.html,它有一个包含iframe1.html的iframe。反过来,iframe1.html包含一个包含iframe2.html的iframe。我希望这是你想到的那种设置。



基本上,你可以使用iframe.contentWindow.myfunc();

然后使用contentWindow.document,您可以访问第二级iframe。



示例函数'doit()'调用父,第一个iframe和第二个iframe中的函数。



希望这会有所帮助!

 <!------- test.html --------> 
< html>
< head>
< script type =text / javascript>
function parent_function(){
alert('parent');
}
函数doit(){
parent_function();
document.getElementsByTagName('iframe')[0] .contentWindow.iframe1_function();
document.getElementsByTagName('iframe')[0] .contentWindow.document.getElementsByTagName('iframe')[0] .contentWindow.iframe2_function();
}
< / script>
< / head>
< body>
main
< a href =javascript:doit();> do it< / a>
< iframe src ='iframe1.html'>
< / body>
< / html>

<!------- iframe1.html -------->
< html>
< head>
< script type =text / javascript>
函数iframe1_function(){
alert('iframe1');
}
< / script>
< / head>
< body>
frame1
< iframe src ='iframe2.html'>
< / body>
< / html>

<!------- iframe2.html -------->
< html>
< head>
< script type =text / javascript>
函数iframe2_function(){
alert('iframe2');
}
< / script>
< / head>
< body>
frame2
< / body>
< / html>


I currently have a page structure that consists of a page(Parent) that includes an iframe(iframe0) and inside that iframe I have another iframe(iframe1). In iframe1 I have a javascript function that I am trying to call from Parent. In Firefox/Chrome/Safari I am able to call this function with the following code:

 frames["iframe0"]["iframe1"].functionName();

However, in Internet Explorer the above code does not work and it returns the error "Object doesn't support this property or method". I have tried some other ways to access the method with them all returning the same error.

 window.frames.iframe0[iframe1].functionName();
 window.iframe0.iframe1.functionName();
 window.frames.iframe0.frames.iframe1.functionName();

I even tried calling a function in iframe0 that called the function in iframe1 and that didn't even work.

Anyone have any idea on how to access a javascript function that is nested in an iframe that is 2 levels deep?

Thanks.

Update: After looking into the problem further, I have found that the problem I am dealing with is not related what I have asked. The answer ylebre gave below answers the question I have asked, and there for will me marked as the answer. I will probably start another question describing my problem in more detail.

解决方案

I've provided an example using 3 HTML files. The outermost is test.html which has an iframe containing iframe1.html. In turn, iframe1.html contains an iframe containing iframe2.html. I'm hoping this is the kind of setup that you have in mind.

Basicly, you can call the iframe function using iframe.contentWindow.myfunc();

Using contentWindow.document you can then access the second level iframe.

The example function 'doit()' calls a function in the parent, first iframe and second iframe.

Hope this helps!

<!------- test.html -------->
<html>
<head>
    <script type="text/javascript">
        function parent_function() {
            alert('parent');
        }
        function doit() {
            parent_function();
            document.getElementsByTagName('iframe')[0].contentWindow.iframe1_function();
            document.getElementsByTagName('iframe')[0].contentWindow.document.getElementsByTagName('iframe')[0].contentWindow.iframe2_function();
        }
    </script>
</head>
<body>
main
<a href="javascript:doit();">do it</a>
<iframe src='iframe1.html'>
</body>
</html>

<!------- iframe1.html -------->
<html>
<head>
    <script type="text/javascript">
        function iframe1_function() {
            alert('iframe1');
        }
    </script>
</head>
<body>
frame1
<iframe src='iframe2.html'>
</body>
</html>

<!------- iframe2.html -------->
<html>
<head>
    <script type="text/javascript">
        function iframe2_function() {
            alert('iframe2');
        }
    </script>
</head>
<body>
frame2
</body>
</html>

这篇关于在IE中嵌套iframe中访问javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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