Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call javascript function from outside an iframe

I have one entire html openning inside an iframe that contains a javascript function getData().Now I am not sure how to call getData() from outside that frame.Also is it possible to call it from an external javascript file ?

like image 677
Hector Barbossa Avatar asked Feb 28 '11 10:02

Hector Barbossa


People also ask

Can iframe call function in parent?

This method safely enables cross-origin communication. And if you have access to parent page code then any parent method can be called as well as any data can be passed directly from Iframe.

Can you run Javascript in an iframe?

Calling a parent JS function from iframe is possible, but only when both the parent and the page loaded in the iframe are from same domain i.e. example.com , and both are using same protocol i.e. both are either on http:// or https:// .

Can you communicate with iframe?

All you have to do is first dispatch an event from the iframe to the parent that notifies the parent that the iframe is loaded (essentially a "ready message"). The parent will be listening for messages and if it receives the "ready message" event, it can then reply to the iframe with whatever message you want to send.

Can iframe access parent windows?

The window will get opened by using the iframe, and the communication could be done with the parent window from the child window. To call a parent window function, use the following syntax and also refer to the code given below.


2 Answers

You can get a reference to the frame window object from the window.frames property. See https://developer.mozilla.org/en/DOM/window.frames

UPDATE:

You can access the global context of a named iframe with window[framename]. e.g:

<iframe src="data.html" name="data"></iframe>

<script>
var myData = window.data.getData();
</script>

Although you will need to make sure the iframe has loaded.

In jQuery you can use the contents method if you want access to the iframe DOM:

$("iframe").contents()

All this is assuming the frame hosted within the same domain.

UPDATE[2]:

You asked if it is possible to call the getData function from an external js file. The answer is yes (if I understand you correctly). Here is an example:

<html>
<head>
    <meta charset="utf-8">
    <title>parent page</title>
</head>
<body>

    <iframe src="data.html" name="data"></iframe>
    <script src="getdata.js"></script>

</body>
</html>

Then in the getdata.js file you have:

var dataFrame = window.data;

// when the frame has loaded then call getData()
dataFrame.onload = function () {
    var myData = dataFrame.getData();
    // do something with myData..
}

Hope this answers your question :)

like image 69
johnhunter Avatar answered Nov 03 '22 19:11

johnhunter


In certain situation there could be a neccessity of calling a javascript function inside an iframe from the parent document, and vice versa ie; calling a javascript function in parent document from the iframe.

For example; the parent document have an iframe with id attribute ‘iFrameId‘, and the function ‘functionInIframe()‘ is defined in that iframe document. Following code can call that iframe function from the parent document itself.

document.getElementById('iFrameId').contentWindow.functionInIframe();

And following code can call the function defined in parent document(functionInParent()) from the iframe itself.

parent.functionInParent();

This way javascript can interact between parent document and iframe.

This is the original post.

like image 1
Lalit Kumar Maurya Avatar answered Nov 03 '22 18:11

Lalit Kumar Maurya