Iframe高度自適應(兼容IE/Firefox、同域/跨域)

看轉貼吧 javascript

可是須要說明的是,就是你們不要覺得能夠用這種方法來控制別人的網頁,不行的 php

這個跨域雖然說域名不同,但必須都是你的,也就是你能夠修改的才行,這個跨域這種方法能夠實現,可是若是是別人的網頁,你不能修改就不行了 html

在實際的項目進行中,不少地方可能因爲歷史緣由不得不去使用iframe,包括目前正火熱的應用開發也是如此。 java

隨之而來的就是在實際使用iframe中,會遇到iframe高度的問題,因爲被嵌套的頁面長度不固定而顯示出來的滾動條,不只影響美觀,還會對用戶操做帶來不便。因而自動調整iframe的高度就成爲本文的重點。 跨域

採用JavaScript來控制iframe元素的高度是iframe高度自適應的關鍵,同時因爲JavaScript對不一樣域名下權限的控制,引起出同域、跨域兩種狀況。 瀏覽器

同域時Iframe高度自適應
下面的代碼兼容IE/Firefox瀏覽器,控制id爲「iframeid」的iframe的高度,經過JavaScript取得被嵌套頁面最終高度,而後在主頁面進行設置來實現。 app

代碼以下,可複製。另外,請注意此解決方案僅供同域名下使用。 測試

 
<script type="text/javascript">
 function SetCwinHeight(){
  var iframeid=document.getElementById("iframeid"); //iframe id
  if (document.getElementById){
   if (iframeid && !window.opera){
    if (iframeid.contentDocument && iframeid.contentDocument.body.offsetHeight){
     iframeid.height = iframeid.contentDocument.body.offsetHeight;
    }else if(iframeid.Document && iframeid.Document.body.scrollHeight){
     iframeid.height = iframeid.Document.body.scrollHeight;
    }
   }
  }
 }
</script>
<iframe width="100%" id="iframeid" onload="Javascript:SetCwinHeight()" height="1" frameborder="0" src="kimi.php"></iframe>

跨域時Iframe高度自適應
在主頁面和被嵌套的iframe爲不一樣域名的時候,就稍微麻煩一些,須要避開JavaScript的跨域限制。 url

原理:現有iframe主頁面main.html、被iframe嵌套頁面iframe.html、iframe中介頁面agent.html三個,經過main.html(域名爲http://www.ccvita.com)嵌套iframe.html(域名爲:http://www.phpq.net),當用戶瀏覽時執行iframe.html中的JavaScript代碼設置iframeC的scr地址中加入iframe頁面的高度,agent.html(域名爲:http://www.ccvita.com)取得傳遞的高度,經過JavaScript設置main.html中iframe的高度。最終實現預期的目標。 spa

演示地址:http://www.ccvita.com/usr/uploads/demo/iframe/main.html
代碼下載:http://www.ccvita.com/usr/uploads/demo/iframe/iframe.zip

iframe主頁面main.html

 
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>iframe主頁面</title></head>
<body>
<div style="border:1px solid #ccc;padding:10px;"><iframe id="frame_content"  name="frame_content" src="iframe.html" width="100%" height="0" scrolling="no" frameborder="0"></iframe></div><br />尾部<br /></body>
</html>

iframe嵌套頁面iframe.html

 
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>被iframe嵌套頁面</title></head>
<body>
文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />文字<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><iframe id="iframeC" name="iframeC" src="" width="0" height="0" style="display:none;" ></iframe>
<script type="text/javascript">
function sethash(){
    hashH = document.documentElement.scrollHeight;
    urlC = "agent.html";
    document.getElementById("iframeC").src=urlC+"#"+hashH;
}
window.onload=sethash;
</script>
</body>
</html>

iframe中介頁面agent.html

 
< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>iframe中介頁面</title></head>
<body>
<script>
function  pseth() {
    var iObj = parent.parent.document.getElementById('frame_content');
    iObjH = parent.parent.frames["frame_content"].frames["iframeC"].location.hash;
    iObj.style.height = iObjH.split("#")[1]+"px";
}
pseth();
</script>
</body>
</html>



UPDATE:長期以來一直有網友說方案不能跨域,今天我從新又測試了下,肯定在 IE六、IE七、IE八、IE九、Firefox全系列、Chrome全系列 都可以成功跨域控制高度。請注意如下要點
  • 第一,修改main.html文件中iframe的src地址爲須要跨域的域名(好比ccvita.sinaapp.com)
  • 第二,修改iframe.html文件中的urlC值爲源域名(好比www.ccvita.com)這點最重要
相關文章
相關標籤/搜索