1

How can i call iframe function in parent window, i did something like below but not seems working in firefox. Same code working perfectly in chrome.

window.frames["original_preview_iframe"].window.exportAndView(img_id);
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
Mangesh Narayankar
  • 591
  • 2
  • 5
  • 5

4 Answers4

3

i think you have to use

document.getElementById('target_Frame').contentWindow.callingtargetFunction();

otherwise use this url describes solution for your problem

Invoking JavaScript code in an iframe from the parent page

Community
  • 1
  • 1
radha krishna
  • 230
  • 1
  • 4
1

Try to not type window after you 'selected' the iframe:

window.frames["original_preview_iframe"].exportAndView(img_id);
thebreiflabb
  • 1,504
  • 10
  • 19
1

Would suggest this https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

Clear wiki example that worked for me:

var o = document.getElementsByTagName('iframe')[0];
o.contentWindow.postMessage('Hello B', 'http://example.com/');

And then in the iframe:

function receiver(event) {
    if (event.origin == 'http://example.net') {
        if (event.data == 'Hello B') {
            event.source.postMessage('Hello A, how are you?', event.origin);
        }
        else {
            alert(event.data);
        }
    }
}
window.addEventListener('message', receiver, false);

(https://en.wikipedia.org/wiki/Web_Messaging.)

Mihkel L.
  • 1,543
  • 1
  • 27
  • 42
0

There are several ways to call the iframe function.
We assume you iframe id is original_preview_iframe

Way 1

You can use document.getElementById("original_preview_iframe").contentWindow.exportAndView() to trigger.

Way 2

Use window.frames.
window.frames is an array, you can set the iframe name with window.name="this is iframe test" in "test.html"
Then you can iterator the array, and compare the name, then trigger it.

for (let i = 0; i < window.frames.length; i++) {
    if (window.frames[i].name === "this is iframe test") {
        window.frames[i].exportAndView()
    }
}

Way 3

Use postMessage.
In the way1 and way2, you need to assign function in the window object.

<body>
<script>
// this one
window.exportAndView = function(){}
// or this one
function exportAndView(){}
</script>
</body>

In the Way3, you can hide the exportAndView then you also can trigger it.
Here is an example.

// a.html
<html>
<body>
        <iframe id="original_preview_iframe" src="/b.html">
        </iframe>
        <script>
            // let postMessage trigger after b.html load
            setTimeout(function(){
                document.getElementById("original_preview_iframe").contentWindow.postMessage({data: "hi"});
            }, 500)
        </script>
</body>
</html>

// b.html (iframe html)
<html>
<body>
    <script>
        (function() {
            function exportAndView() {
                console.log("test");
            }
            window.addEventListener("message", (event) => {
                exportAndView()
            })
        })()
    </script>
</body>
</html>

Then in a.html, you can try use the way1 or way2, like document.getElementById("original_preview_iframe").contentWindow.exportAndView().
exportAndView will not be called becuase the scope problem.

Jack Yu
  • 2,190
  • 1
  • 8
  • 14