---
title: "iframe高度解析"
author: "无心徘徊"
date: "2011-09-07T10:48:54Z"
tags: []
categories: ["Web前端"]
slug: "iframe"
draft: false
lastmod: "2011-09-07T10:51:24Z"
id: 1167318
url: "https://crazy-wind1.iteye.com/blog/1167318"
---

```java
var cwin=document.getElementById("cwin");
				/**
				if (document.getElementById)
				{
				if (cwin && !window.opera)
				{
				if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight){
					cwin.height = cwin.contentDocument.body.offsetHeight; 
					}
				else if(cwin.Document && cwin.Document.body.scrollHeight){
					cwin.height = cwin.Document.body.scrollHeight+1;
					}
				}
				}*/
				var pheight = parent.document.getElementById("mainframe").contentWindow.document.body.scrollHeight;
				var cheight = cwin.contentWindow.document.body.scrollHeight;
				 parent.document.getElementById("mainframe").height = pheight + cheight;
				cwin.height=cheight;
```


注释部分为iframe自适应效果
contentWindow.document.body.scrollHeight 得到frame所在页面的滚动高度（即总长）
.contentWindow.document.documentElement.scrollHeight 得到frame的显示高度（有滚动条的情况下）