Reputation:
How to invoke a function in iframe using JS or how to even target the iframe? I've tried using document.getElementByID("myIframe")
, but it didn't seem to work. Below is a basic form of the HTML:
<html>
<head>
<script>
function runFunction() {
var win = document.getElementByID("myIframe");
}
</script>
</head>
<body onload="runFunction()">
<iframe id="myIframe" name="myIframe" src="http://www.myothersite.com/"></iframe>
</body>
</html>
My other site has a function named getEvents() that I would like to somehow invoke. Any help would be greatly appreciated.
Upvotes: 0
Views: 65
Reputation: 133453
You can use the contentWindow property which can be used to access the window object that belongs to a iframe
element.
The
contentWindow
property returns the Window object by an iFrame element (through the Window object, you can access the document object and then any one of the document's elements). This DOM attribute is read-only.
document.getElementById("myIframe").contentWindow.getEvents();
Upvotes: 1
Reputation: 1374
You should be able to access the iframe
and invoke the function using the following:
// reference to window in iframe with id or name 'myIframe'
var win = window.frames['myIframe'];
// invoke function in win
win.getEvents();
Upvotes: 1