2014-05-22 49 views
0

我有從不同域加載的內容的iframe:Iframe內容位於不同域時,如何根據其內容更改Iframe的高度?

<div class="iframe"> 
    <script language="javascript" type="text/javascript"> 
     var queryString = unescape(document.location.search.split('?id=').join('')); 
     document.write("<iframe id=\"frame\" onload=\"resize()\" src=\"http://differentdomain.com/" + queryString + "\" scrolling=\"no\" width=\"850\" height=\"450\" frameborder=0></iframe>"); 
    </script> 
</div> 

的iframe的內容可以是不同的高度。

我有一個方法resize()當iframe中加載如下執行:

function resize() { 
    var iFrameID = document.getElementById("frame"); 
    if (iFrameID) { 
     iFrameID.height = ""; 
     iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px"; 
    } 
} 

當運行的網頁我有一個訪問被拒絕的錯誤,我猜想是因爲跨域的問題。

我該如何解決這個問題,以便我的框架高度根據其內容動態更改?

謝謝你的

+0

您是否搜索過? – epascarello

+0

如果你[可以控制頁面](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy),如果你不能,沒有什麼可以做的,因爲[同源策略](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript)。 – Teemu

回答

1

你不能。 除非您也控制其他域,您可以禁用保護。