access iframe content javascript code example
Example 1: javascript search on docuemt for text on iframe
var myIFrame = document.getElementById("myIframe");
var content = myIFrame.contentWindow.document.body.innerHTML;
Example 2: js set iframe src
<script type="text/javascript">
function iframeDidLoad() {
alert('Done');
}
function newSite() {
var sites = ['http://getprismatic.com',
'http://gizmodo.com/',
'http://lifehacker.com/']
document.getElementById('myIframe').src = sites[Math.floor(Math.random() * sites.length)];
}
</script>
<input type="button" value="Change site" onClick="newSite()" />
<iframe id="myIframe" src="http://getprismatic.com/" onLoad="iframeDidLoad();"></iframe>
Example 3: use javascript to control iframe elements
<iframe id="myIFrame" src="thispage.html"
width="100%" height="600"
frameBorder="2">
</iframe>
const iFrame = document.getElementById('myIFrame');
const buttonInIFrame = iFrame.contentWindow.document.getElementById('myButton');
iFrame.contentWindow.yourFunction();