javascrip set source of iframe code example
Example 1: 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 2: 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();