Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  169] [ 3]  / answers: 1 / hits: 16391  / 14 Years ago, mon, february 28, 2011, 12:00:00

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 ?


More From » iframe

 Answers
18

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 :)


[#93542] Saturday, February 26, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
reed

Total Points: 725
Total Questions: 85
Total Answers: 89

Location: Singapore
Member since Sat, Jul 25, 2020
4 Years ago
;