最直覺的做法是讀取 iframe 內部文件的 body 高度,再回寫給 iframe 的 height:
var iframeids = ['memberIndexIframe','inquiryCenterIframe','everychinaBbsIframe'];
var iframehide = "yes";
function dynIframeSize(){
var dyniframe = [];
for(var i=0;i<iframeids.length;i++){
if(!document.getElementById) return false;
dyniframe[i] = document.getElementById(iframeids[i]);
if(dyniframe[i] && !window.opera){
dyniframe[i].style.display = "block";
if(dyniframe[i].contentDocument && dyniframe[i].contentDocument.body.offsetHeight){
dyniframe[i].height = dyniframe[i].contentDocument.body.offsetHeight;
}else if(dyniframe[i].Document && dyniframe[i].Document.body.scrollHeight){
dyniframe[i].height = dyniframe[i].Document.body.scrollHeight;
}
}
if((document.all || document.getElement) && iframeids == "no") {
var tempobj = document.all?document.all[iframeids[i]] : document.getElementById(iframeids[i]);
tempobj.style.display = "block";
}
}
}
}
if (window.addEventListener)
window.addEventListener("load", dynIframeSize, false)
else if (window.attachEvent)
window.attachEvent("onload", dynIframeSize)
else {
window.onload = dynIframeSize;
}
存成 iframe.js 後於頁面引用即可。但這段程式有個前提:必須同源,否則 contentDocument 會因跨來源安全政策而無法存取,程式碼裡那個 iframeids == "no" 的判斷也明顯是筆誤,應該比對的是某個控制項的值。
跨域時無法直接讀對方的 DOM,只能請對方「主動把高度告訴你」。常見作法是在對方頁面裡隱藏一個指向自己網域的代理頁,透過 URL 的 hash 部分傳值——因為改變 hash 不會觸發跨域導航限制。
第一步,主頁面 a.html 放入 iframe:
<BODY>
<iframe width="100%" id="a_iframe" name="a_iframe" src="https://對方域名/b.html"
frameborder="no" border="0px" scrolling="no" allowtransparency="yes" ></iframe>
</BODY>
第二步,在自己的網域下建一個 agent.html,負責接收並套用高度:
<script type="text/javascript">
// 取得 a.html 中的 a_iframe
var a_iframe = parent.parent.document.getElementById("a_iframe");
// location.hash 用來取頁面 hash 值,這個值由 b.html 的 JS 改寫
var hash_url = window.location.hash;
// 取出 b.html 傳過來的 height
var hash_height = hash_url.split("#")[1]+"px";
// 調整高度,達到自適應
a_iframe.height = hash_height;
</script>
第三步,請對方在 b.html 加入:
<iframe id="b_iframe" width="100%" src="https://我的域名/../agent.html" style="display:none">
</iframe>
<script type="text/javascript">
var b_height = Math.max(document.body.scrollHeight,document.body.clientHeight);
var b_iframe = document.getElementById("b_iframe");
b_iframe.src = b_iframe.src+"#"+b_height;
</script>
用 Math.max 比對「可視區域高度」與「內文完整高度」取較大值,寫進 src 的 hash,agent.html 讀到後就能設定 a.html 中 iframe 的高度。原始實作在 IE 下測試無誤,其他瀏覽器未逐一驗證。
第一,這套方案要求雙方合作改程式碼。若對方不願配合,跨域自適應幾乎無解——這正是同源政策的目的。
第二,最乾淨的現代解法是 postMessage:父頁監聽 message 事件並校驗 event.origin,子頁在內容變化時 postMessage 自己的高度給 parent。它不必來回塞隱藏 iframe,也不必約定 hash 格式,安全性也更可控。
第三,無論哪種方案,內容高度會隨視窗寬度、圖片載入、字型載入而改變,因此除了 load,還應在 resize 與 ResizeObserver 上重複計算,否則使用者一縮放視窗就會出現雙重捲軸。
<script>
var strInfo=" " ;
strInfo+=" \r\n網頁可見區域寬:" + document.body.clientWidth;
strInfo+=" \r\n網頁可見區域高:" + document.body.clientHeight;
strInfo+=" \r\n網頁可見區域寬:" + document.body.offsetWidth+" (包括邊線的寬)";
strInfo+=" \r\n網頁可見區域高:" + document.body.offsetHeight+" (包括邊線的寬)";
strInfo+=" \r\n網頁正文全文寬:" + document.body.scrollWidth;
strInfo+=" \r\n網頁正文全文高:" + document.body.scrollHeight;
strInfo+=" \r\n網頁被卷去的高:" + document.body.scrollTop;
strInfo+=" \r\n網頁被卷去的左:" + document.body.scrollLeft;
strInfo+=" \r\n網頁正文部分上:" + window.screenTop;
strInfo+=" \r\n網頁正文部分左:" + window.screenLeft;
strInfo+=" \r\n屏幕分辨率的高:" + window.screen.height;
strInfo+=" \r\n屏幕分辨率的寬:" + window.screen.width;
strInfo+=" \r\n屏幕可用工作區高度:" + window.screen.availHeight;
strInfo+=" \r\n屏幕可用工作區寬度:" + window.screen.availWidth;
window.confirm(strInfo);
</script>
把這幾個值攤開來看一次,clientHeight/offsetHeight/scrollHeight 三者到底差在哪,就再也不會混淆了。