---
title: "父页面iframe高度自动适应子页面iframe高度"
author: "leyoo"
date: "2011-06-10T10:51:52Z"
tags: []
categories: ["Web前端"]
slug: "iframe-iframe"
draft: false
lastmod: "2011-06-10T10:51:52Z"
id: 1075112
url: "https://leyoo.iteye.com/blog/1075112"
---

function initParentHeight()
	{
	var div = window.parent.document.getElementById('mainFrame');
	div.style.height=window.document.body.scrollHeight||window.document.body.offsetHeight+5;
	}
	
	function SetWinHeight(obj)
	{
	  var win=obj;
	   if (win && !window.opera)
	   {
	     if (win.contentWindow && win.contentWindow.document.body.offsetHeight)   //ie  8
	     {
	        var height;
	        try{
				var bheight=win.contentWindow.document.body.scrollHeight;
				var dheight=win.contentWindow.document.documentElement.scrollHeight;
			    height=Math.max(bheight,dheight);
				}catch(ex){}
	       win.style.height = height;
	     }
	     else if(win.Document && win.Document.body.scrollHeight)//ie  6
	     {
	      win.style.height = win.Document.body.scrollHeight;
	     }
	   }
	   initParentHeight();
	}


<iframe id="show_detail" onload="Javascript:SetWinHeight(this);" src="businessAction.do?method=init_CZ_details"  width="700" name="cz" frameborder="0" scrolling="no" style="overflow-x:hidden; " ></iframe>