2011-07-06 18 views
0

這是我的功能。並在onload事件處理函數我試圖計算iframescrollHeight。 但我只收到錯誤消息。有人可以幫忙嗎?計算實際的iframe高度時出錯

function showHBewertung(W3CElement, objCode) { 
     var iframe = document.createElement("iframe"); 
      iframe.className = "bewertung_iframe" 
      iframe.src = "http://example.com?giatacode="+objCode; 
      W3CElement.appendChild(iframe); 

      iframe.onload = function() { 
       var height=iframe.contentWindow.document.body.scrollHeight; 
       iframe.style.cssText = "width:100%;height:" + height + "px"; 
      } 

    } 
+1

什麼錯誤信息詳細說? – reporter

+0

螢火蟲顯示什麼? – Rahul

+0

錯誤:權限被拒絕訪問屬性「文檔」 – user160820

回答

1

如果是在同一域(並使用相同的端口和協議)作爲父頁面,您只能訪問文件。也就是說,要訪問http://example.com/ iframe文檔,您的iframe必須位於http://example.com/頁面上。如果你這樣做,你可以訪問的iframe文檔的scrollHeightiframe.contentDocument.body.scrollHeight

function showHBewertung(W3CElement, objCode) { 
    var iframe = document.createElement("iframe"); 
    iframe.className = "bewertung_iframe"; 
    iframe.src = "http://example.com?giatacode="+objCode; 
    W3CElement.appendChild(iframe); 
    iframe.onload = function() { 
     var height = iframe.contentDocument.body.scrollHeight; 
     iframe.style.cssText = "width:100%;height:" + height + "px"; 
    }; 
}