Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a javascript function in a nested iframe in IE

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.

like image 666
Tim Banks Avatar asked Dec 30 '22 23:12

Tim Banks


1 Answers

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>
like image 101
ylebre Avatar answered Jan 12 '23 07:01

ylebre