---
title: "iframe高度自适应"
author: "limon87"
date: "2013-05-04T14:03:25Z"
tags: []
categories: ["Web前端"]
slug: "iframe"
draft: false
lastmod: "2013-05-04T14:03:25Z"
id: 1859701
url: "https://limon87.iteye.com/blog/1859701"
---

```
var adjustIframe = function (id) {
    var iframe = document.getElementById(id)
    var idoc = iframe.contentWindow && iframe.contentWindow.document || iframe.contentDocument;
    var callback = function () {
        var iheight = Math.max(idoc.body.scrollHeight, idoc.documentElement.scrollHeight); //取得其高
        iframe.style.height = iheight + "px";
    }
    if (iframe.attachEvent) {
        iframe.attachEvent("onload", callback);
    } else {
        iframe.onload = callback
    }
}
<iframe id="js_sub_web" width="80%" frameborder="0" scrolling="no" allowTransparency="true" src="http://www.cnblogs.com/rubylouvre/archive/2009/09/15/1566722.html">
</iframe>

使用方法：
window.onload = function(){
     adjustIframe("js_sub_web");
}
```

 from:http://www.cnblogs.com/rubylouvre/archive/2011/01/05/1926226.html