I’ve got a bug in IE8 that I can’t figure out. I’m trying to call a function that is inside an iframe from outside the iframe. The result of the code is “Object doesn’t support this property or method”. Here is the code:
<html><head>
<script type="text/javascript">
setTimeout(function () {
document.getElementById('mypage').contentWindow.MyFunction('myVar') }, 10000);
</script>
</head><body>
.....
<iframe id="mypage" src="myfile.php">
<script type="text/javascript">
function MyFunction(myvar) {
.....
}
</script>
</iframe>
.....
</body></html>
your javascript will execute well before the iframe is even known, let alone loaded. try adding your javascript to the iframe’s load event rather than directly calling it. You’d also have to do this in dom ready to insure the iframe has loaded to the dom.
Something like (excuse the jQuery, but it’s what I know):
And one last note, this may or may not be required (depending on your page more than anything else). If the script is slow to load, or loads late in the page load cycle of the frame, you may need to setInterval to test if it has loaded yet. You basically need to insure your calling the function after the javascript that creates it is executed.