How to set the scr in an iframe with javascript

Categories:  Web
Labels:  html, javascript

How to dynamically set the scr in an iframe javascript, with the following code you can make changes to the scr in an iframe using javascript, very usefull in html web desing.

Html

<input type="button" name="calendarSelection" title="change" value="Change site" onClick="newSite()" />
<iframe id="myIframe" src="http://getprismatic.com/" onLoad="iframeDidLoad();"></iframe>

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)];
}

Here’s the jQuery way to do it:

$('#calendar').attr('src', loc);

Source

Related Posts: