2012-08-16 82 views
0

我最近得到了臭名昭着的問題,訪問其父母的iframe。我試圖在更簡單的htmls中模仿它。所以我創建了兩個文件,parent.htm,child.htm。即使它們在本地執行,我也會遇到一般錯誤。訪問iFrame父

Unsafe JavaScript attempt to access frame with URL file:///C:/Users/kvaradar/Desktop/New%20folder/Parent.htm from frame with URL file:///C:/Users/kvaradar/Desktop/New%20folder/Child.htm. Domains, protocols and ports must match. 

那麼,它被告知父母和孩子必須在同一個域。但就我而言,我將它們作爲本地文件 - Parent.htm,Child.htm。

我假設他們應該在同一個域。爲什麼在這種情況下我得到這個錯誤?我錯過了什麼。

這是父級HTML。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<body> 
<span id="myParentWindow">Some content of parent frame's span.</span> 
<iframe src="Child.htm"></iframe> 
</body> 
</html> 

孩子的HTML。

<html xmlns="http://www.w3.org/1999/xhtml"> 

<script type="text/javascript"> 
    function loadChild() { 
     document.getElementById('myChild').innerHTML 
      = window.parent.document.getElementById('myParent').innerHtml; 
    } 
</script> 
<body onload="loadChild()"> 
    <div style="border:1px solid green;height:500px; min-height:500px"> 

    Some content of the child frame. 
    In the following span, I am trying to fill the parent's span content through javascript 

    <span id="myChild"></span> 
    </div> 
</body> 
</html> 

回答

0

我不能重現說錯誤,但loadChild功能有2個錯誤:

  • 它是指myParent,而不是myParentWindow

  • 它指的是innerHtml而不是`innerHTML'。

固定的腳本:

function loadChild() { 
    document.getElementById('myChild').innerHTML 
    = window.parent.document.getElementById('myParentWindow').innerHTML; 
} 

即使上述修正後,我不能reprodure的錯誤在Firefox 14一切都沒有任何腳本警告或錯誤工作正常。

+0

嗨周杰倫,謝謝你的回覆。我同意loadChild()有兩個錯誤。但是用Firefox你通常不會看到錯誤。您需要轉到錯誤控制檯才能看到它。您可以從工具菜單 - > Web開發人員 - >錯誤控制檯訪問它。 – SaravananArumugam 2012-08-21 20:49:47

+0

除非您通過'try ... catch'塊處理它,並使用'alert'顯示錯誤,否則錯誤將始終放在控制檯日誌中。致命錯誤會導致代碼無條件終止,其餘代碼將永遠不會執行。非致命錯誤不會導致代碼終止,但它會在代碼後面的其他部分導致致命錯誤,這是很難調試的。在這種情況下,這是一個漸進的錯誤。直到錯誤足夠嚴重,您纔會注意到它。 – Jay 2012-08-22 05:50:46