2014-05-17 65 views
7

如果以下方式獲得contentWindow使用,該值是不確定的IFRAME contentWindow是不確定的,當使用window.frames [名]進入

<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> 

如果使用像下面的另一種方式,它工作正常:

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

我在FF 29.0.1,chrome 34,IE11上測試過,它們都以相同的方式工作。

所以我有兩個問題:

  1. 爲什麼第一種方式無法獲得contentWindow值
  2. iframe.contentWindow是在所有的瀏覽器兼容嗎?

回答

10
window.frames["frame1"]; 

contentWindow,得到它是一個命名窗口,你的情況是同樣的事情

document.getElementById("frame1").contentWindow 

FIDDLE

+0

所以contentWindow是在所有的瀏覽器包括IE8 +兼容? – jason

+0

應該是很漂亮的跨瀏覽器,一些瀏覽器用來使用contentDocument,但我認爲contentWindow今天到處都是。 – adeneo

相關問題