`
心语2012
  • 浏览: 44834 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

设置iframe高度的问题

    博客分类:
  • js
阅读更多
对于iframe高度的问题,需要分两类,第一种是同域的父页面和子页面解决方法是
假如iframe的ID是iframeExp
var newIframe = docment.getElementById("iframeExp");
function setHeight(){
    var doc=newIframe.contentWindow.document;
    var maxHeight=Math.max(doc.documentElement.scrollHeight,doc.body.scrollHeight);
    if(maxHeight!=getStyle(newIframe,"height")){
         newIframe.style.height=maxHeight+"px";
     }
}

function getStyle(obj,attr){
   if(obj.currentStyle){
      return obj.currentStyle[attr];
    }else{
       return getCumputerStyle(obj,false)[attr];
     }
}

if(newIframe){
   setInterval(setHeight(),300);
}

第二种是跨域的的父页面和子页面
假如父页面是A,子页面是B,需要一个代理商页面是C。
其中A和C是同域,A嵌套B,B嵌套C,
在B中需要计算自己的高度然后通过URL传给C,然后C可以访问A把高度告诉A
在B中
  var getHeight = function(){
        return Math.max(document.documentElement.scrollHeight,document.body.scrollHeight);
    };
agent_iframe.src = "C的地址?a=" + Math.random() + "#" + getHeight();

在C中
  window.top.document.getElementById("frame_content_parent").height = parseInt(window.location.hash.substring(1),10);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics