Iframe contentWindow undefined when using window.frames [name] to access

If you use the following path to get 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 you use a different method, as shown below, it works fine:

 var frame1 = document.getElementById("frame1"); console.info(frame1.contentWindow); 

I tested FF 29.0.1, chrome 34, IE11, they all work the same way.

I have two questions:

  • why the first method cannot get the value of contentWindow
  • iframe.contentWindow compatible in all browsers?
+7
javascript html iframe
source share
1 answer
 window.frames["frame1"]; 

is contentWindow , it gets a named window, and in your case it is the same as

 document.getElementById("frame1").contentWindow 

Fiddle

+10
source share

All Articles