If use following way to get the contentWindow, the value is undefined
<html>
<head>
<title>iframe test</title>
</head>
<body>
<iframe id="frame1" src="frame1.html" name="frame1"></iframe>
<script>
document.body.onload = function() {
console.info("index loaded");
var frame1 = window.frames["frame1"];
console.info(frame1.contentWindow);
}
</script>
</body>
</html>
If use the other way like following, it works fine:
var frame1 = document.getElementById("frame1");
console.info(frame1.contentWindow);
I tested on FF 29.0.1, chrome 34, IE11, they all work the same way.
So I have two questions:
The contentWindow property returns the Window object of an HTMLIFrameElement. You can use this Window object to access the iframe's document and its internal DOM. This attribute is read-only, but its properties can be manipulated like the global Window object.
open or a window inside <iframe> , and that window comes from the same origin, then we have full access to that window. otherwise, if it comes from another origin, then we can't access the content of that window: variables, document, anything.
window.frames["frame1"];
is the contentWindow
, it gets a named window, and in your case it's the same thing as
document.getElementById("frame1").contentWindow
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With