Mankament Gra Mankament Gra - 1 month ago 4
Javascript Question

Iframe dosent work

I have code:

<iframe src="in1.html" width="400" height="300" id="f1">
<p>Your browser does not support iframes.</p>
</iframe>

<script>
var childFrame = document.getElementById("f1").contentWindow;
childFrame.document.body.style.backgroundColor = "yellow"
</script>


When i load page yellow is show on but then white background is load. Why?

Answer

You'll have to wait for the iframe to load ....

iframe src="in1.html" width="400" height="300" id="f1">
<p>Your browser does not support iframes.</p>
</iframe>

<script>
    var childFrame = document.getElementById("f1");

    childFrame.onload = function() {
        var cWindow = childFrame.contentWindow;
        cWindow.document.body.style.backgroundColor = "yellow";
    }
</script>
Comments