---
title: "怎么让iframe的高度根据所加载页的高度而改变"
author: "highping"
date: "2009-04-06T22:47:47Z"
tags: []
slug: "iframe"
draft: false
lastmod: "2009-04-06T22:47:47Z"
id: 362478
url: "https://highping.iteye.com/blog/362478"
---

解决思路：  
        如果页面没有加载完成是无法知道它的高度的，所以不能直接设置，而必须通过iframe的onload事件来触发脚本，在 iframe 加载完后重新设置它的高为所加载文档的滚动高度。  
        具体步骤：  
        1.包含框架页的代码。

```
<iframe frameborder="0" scrolling="no" name="demo"  
onload="document.all.demo.height=document.frames[’demo’].document.body.scrollHeight" src="demo.htm" width="100%"></iframe> 
```

 

        2.demo.htm页代码。

 

```
<body bgColor="#EEEEEE"> 
<script> 
       document.write(new Array(100).join("1<br>")) 
</script> 
```