在IE中嵌套iframe中访问javascript函数

时间:2022-04-28 15:47:13

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:

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

 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.

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

 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.

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

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

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

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.

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

2 个解决方案

#1


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.

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

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

基本上,您可以使用iframe.contentWindow.myfunc()调用iframe函数;

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

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

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

示例函数'doit()'调用父,第一个iframe和第二个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>

#2


A fast way to select an iframe is to select it in the dom explorer, then in the js console, you can run $0.contentWindow.myFunction()

选择iframe的一种快速方法是在dom explorer中选择它,然后在js控制台中,你可以运行$ 0.contentWindow.myFunction()

#1


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.

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

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

基本上,您可以使用iframe.contentWindow.myfunc()调用iframe函数;

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

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

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

示例函数'doit()'调用父,第一个iframe和第二个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>

#2


A fast way to select an iframe is to select it in the dom explorer, then in the js console, you can run $0.contentWindow.myFunction()

选择iframe的一种快速方法是在dom explorer中选择它,然后在js控制台中,你可以运行$ 0.contentWindow.myFunction()