網頁

2008年12月24日 星期三

處理iframe auto height(Iframe自適應高度)

全部參考:
1.Iframe自動調整高度, 在子網域SubDomain的情形
2.處理iframe auto height(Iframe自適應高度)
3.處理iframe auto height(Iframe自適應高度) - 相關問題紀錄
4.完整的iframe auto height程式(在iframe內容頁中處理)
5.或許你想要 "html include html,兩種工具介紹。"


注意


1.現在會寫下這篇的主要原因在於google chrome中,iframe的高度,會不斷的增高,當不斷按下iframe.content中的連接時,會不斷的reload。
2.這篇無法處理cross site的問題,先前有一篇「Iframe自動調整高度, 在子網域SubDomain的情形
3.請參考處理iframe auto height(Iframe自適應高度) - 相關問題紀錄


處理的過程是


1.是從iframe的onload事件處理
2.或是從iframe content的onload事件處理
3.將iframe.height設成iframe.content.height。

但過程中就會遇到很多問題

Iframe 的onload事件


1.在Linux的Konqueror中似乎只有第一次, 之後內容的改變都不會再onload.
2.Mac的Safari似乎沒有這個onload事件, 因此Iframe先預設高度為2000px.
3.而IE 6; Firefox 2; Opera 9都沒有問題.

這個onload問題決定的是,要在iframe或iframe.content的onload事件來處理

iframe.content.height的取得問題


和height有關的屬性有clientHeight、offsetHeight和scrollHeight,參考 http://www.tsolong.com/post/355.html
1.每個Browser的定義不相同
* 參考同上。

2.當設DOCTYPE後還會產生變化
* 目前的測試是,沒加的話,3種高度Height會依Browser的設定
* 有加的話,不論那一種,3種高度Height都同樣拿到scrollHeight的值。

3.想說用jquery來取得height,也會受DOCTYPE設定的影响
用alert($("body").height()+" "+$("body").outerHeight()+" "+$("body").innerHeight());來測
* 在chrome和firefox中,不論DOCTYPE設什麼,或沒有設,3種高度都同樣拿到scrollHeight的值。
* 在IE7中,DOCTYPE有設,3種高度都同樣拿到scrollHeight的值。
* DOCTYPE沒設,3種高度都同樣拿到像是clientHeight的值。
* 需在每個頁面加入jquery.js

4.chrome中的iframe.document.body.scrollHeight會受iframe.height影响,
5.本文後面還遇到css設定position:absolute影响高度的問題

以上問題試了好幾個小時,覺得這真是個有點難解。

目前的解法


1.在iframe的onload中處理
2.每個iframe content一定要有DOCTYPE(解決chrome的問題,才能用offsetHeight)
3.用offsetHeight的值(解決chrome的問題)
  1. <script type="text/javascript" src="http://of.openfoundry.org/javascripts/prototype.js"></script>
  2. <script type="text/javascript" >
  3. function iframe_auto_height(fid)
  4. {
  5.   var iframe = $(fid);
  6.   var content_height = iframe.contentWindow.document.body.offsetHeight+50;
  7.   content_height = content_height < 400 ? 400 : content_height; //set minimal height
  8.   content_height = typeof content_height == 'number' ? content_height+"px" : content_height
  9.   iframe.setStyle({height:content_height});
  10.   //上一行是Prototype JavaScript framework的語法,所以要先引用prototype.js,而且雖然iframe onload已傳入this,還是得再做一次$(fid),才能使用setStyle。
  11. }
  12. </script>
  13. <iframe onload="iframe_auto_height(this);" src="iframe_content.html" id="of_module" name="of_module" frameborder="0" style="height:200px;"></iframe>

這段code已測試的,Firefox3、IE7、Chrome。
還想在Konqueror、Safari、Opera、IE6中做測試。
不敢保證用offsetHeight在未測試的Browser或舊版本中不會遇到問題。
在iframe.onload中處理,也可能有瀏覽器會遇到問題。

實際運用後又遇到問題


http://of.openfoundry.org/projects/1/sympa
在網站連接sympa(mailing list system)的時候,設高度的功能硬是失效,原來拿到的content.height是0,為什麼是0呢?
經檢查發現以下片段
  1. <style>
  2. #Canvas {
  3. position: absolute;
  4. width: 98%;
  5. margin: 7px 7px 0px 7px;
  6. }
  7. </style>
  8. ......
  9. <body>
  10. <div id="Canvas">
  11.   <div id="Header">
  12.   ..............
  13.   ..............
  14. </div>
  15. </body>

名為Canvas的div將整個網頁包住了,並且設為position: absolute;,造成Canvas像是定在那裏,而整個網頁並沒有高度。(目前只確定Firefox3有這問題,其它Browser沒有測試)

2 則留言:

QQ 提到...

謝謝~超棒的 但目前測試過chrome好像還不行
高度沒有出來

QQ 提到...

弄錯了 是在本地端測試才有問題 放到網路上就OK了 我是說chrome 總之非常感謝~~