1.9.2.IFrame contentWindow property

1.Introduction

  • contentWindow可以回傳由IFrame產生的window object

  • 藉由window object, 可以取得document object以及其任何一個element

2.Usage

<!DOCTYPE html>
<html>
<body>

<iframe id="myframe" src="demo_iframe.htm"></iframe>

<p>Click the button to change the background color of the document contained in the iframe.</p>

<p id="demo"></p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var x = document.getElementById("myframe");
    var y = (x.contentWindow || x.contentDocument);
    if (y.document)y = y.document;
    y.body.style.backgroundColor = "red";
}
</script>

</body>
</html>

Last updated