---
title: "iframe 自适应高度 IE FireFox兼容"
author: "keda413"
date: "2009-04-14T00:47:04Z"
tags: ["C", "C#", "C++", "Firefox", "IE"]
slug: "iframe-ie-firefox"
draft: false
lastmod: "2009-04-14T00:47:04Z"
id: 366873
url: "https://keda413.iteye.com/blog/366873"
---

```
方法一：
function c()

{

var frm = document.getElementById("IframeId");
//document.frames["IframeName"].document获取子页面的document对象，可获得页面的各个对象，但是不能得到iframe标签的属性
document.getElementById("IframeUser")只是得到iframe标签对象，可获得标签的各个属性，但不能得到子页面的各个对象。document.frames为IE下使用，frm.contentDocument为firefox下使用。
var subWeb = document.frames ? document.frames["IframeUserName"].document : frm.contentDocument;

if(frm != null && subWeb != null)

{ frm.height = subWeb.body.scrollHeight;}

} 

方法二：
function d(){
 var frm=document.getElementById("IframeUser");
//frm.contentWindow.document为IE下 使用，获得子页面各个对象
var sub=frm.contentDocument ? frm.contentDocument:frm.contentWindow.document; 
 if(frm!=null&&sub!=null){
  frm.height=sub.body.scrollHeight;
 }
}

 

<iframe marginwidth=0 marginheight=0 src="2.html"   onload="d();" width="100%"    id="IframeId" name="IframeName"    frameborder="0"   scrolling="no"></iframe> 

 
```