Call javascript function from outside iframe

I have one full html discovery inside an iframe that contains the javascript getData () function. Now I'm not sure how to call getData () from outside the scope of this frame. Can you also call it from an external javascript file?

+7
source share
3 answers

You can get a link 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 the named iframe using window[framename] . eg:

 <iframe src="data.html" name="data"></iframe> <script> var myData = window.data.getData(); </script> 

Although you need to make sure that the iframe is loaded.

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

 $("iframe").contents() 

All this assumes that the frame is located inside the same domain .

UPDATE [2]:

You asked if the getData function can be called 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 :)

+10
source

in these cases, you name your iframe and the main object that uses / runs the frame and then uses parent.objectname, in JS everything is an object, and you should be able to call getData ()

quick googling led me to this → http://www.esqsoft.com/javascript_examples/iframe_talks_to_parent/

0
source

In a certain situation, it may be necessary to call the javascript function inside the iframe from the parent document and vice versa; call javascript function in parent document from iframe.

For example, the parent document has an iframe with the id attribute 'iFrameId', and the function 'functionInIframe ()' is defined in this iframe document. The following code can call this iframe function from the parent document itself.

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

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

 parent.functionInParent(); 

This way javascript can interact between the parent document and the iframe.

This is the original message .

0
source

All Articles