dynamically set iframe src

JavascriptJquery

Javascript Problem Overview


I have a program which will dynamically set an iframe src to load pages. I need to hook a event handler for the page completely loaded. How can i do it? Thanks!

Javascript Solutions


Solution 1 - Javascript

<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 at http://jsfiddle.net/MALuP/

Solution 2 - Javascript

Try this:

top.document.getElementById('AppFrame').setAttribute("src",fullPath);

Solution 3 - Javascript

Try this...

function urlChange(url) {
    var site = url+'?toolbar=0&amp;navpanes=0&amp;scrollbar=0';
    document.getElementById('iFrameName').src = site;
}

<a href="javascript:void(0);" onClick="urlChange('www.mypdf.com/test.pdf')">TEST </a>

Solution 4 - Javascript

You should also consider that in some Opera versions onload is fired several times and add some hooks:

// fixing Opera 9.26, 10.00
if (doc.readyState && doc.readyState != 'complete') {
    // Opera fires load event multiple times
    // Even when the DOM is not ready yet
    // this fix should not affect other browsers
    return;
}

// fixing Opera 9.64
if (doc.body && doc.body.innerHTML == "false") {
    // In Opera 9.64 event was fired second time
    // when body.innerHTML changed from false
    // to server response approx. after 1 sec
    return;
}

Code borrowed from Ajax Upload

Solution 5 - Javascript

try this code. then 'formId' div can set the image.

$('#formId').append('<iframe style="width: 100%;height: 500px" src="/document_path/name.jpg"' +
 'title="description"> </iframe> ');

Solution 6 - Javascript

Try this:

document.frames["myiframe"].onload = function(){
   alert("Hello World");
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDagangView Question on Stackoverflow
Solution 1 - JavascriptsimonbsView Answer on Stackoverflow
Solution 2 - JavascriptDeepikaView Answer on Stackoverflow
Solution 3 - JavascriptSunnyView Answer on Stackoverflow
Solution 4 - JavascriptSuorView Answer on Stackoverflow
Solution 5 - JavascriptChamod WijesenaView Answer on Stackoverflow
Solution 6 - JavascriptHeadshotaView Answer on Stackoverflow