anonym anonym - 5 months ago 24
HTML Question

SRC attributes in IFRAME change with onload function

if I have the "src" attributes in IFRAME change is the "onload" Function not executed. It works ONLY when first called.

JavaScript:

function dialog () {
document.getElementById('result').src = "myurl";
}

function loading() {
document.getElementById('loading').style.display = "none";
document.getElementById('result').style.display = "block";
}





HTML:

<div id="loading"><img src="loading.gif"/></div>
<iframe style="display:none;" id="result" onload="loading();" src="myurl"/></iframe>


Thank you!

Answer

Does your page domain and iframe domain the same? If they are different you can use CORS to access iframe events.


EDIT

<script>

    function dialog() {
        document.getElementById('result').src = "http://jsfiddle.net";
    }
    function loading() {
        alert('Loading finished');
        document.getElementById('loading').style.display = "none";
        document.getElementById('result').style.display = "block";
    }

</script>

<div id="loading">Loading</div>
<iframe  style="display:none;" id="result" onload="loading();" src="http://jsfiddle.net"/>

It works, live demo -- http://jsfiddle.net/xf2LgLsg/ I believe you write JS after HTML, so this may be a problem.