main.htm:
1: <meta content="text/html; charset=gb2312" http-equiv="Content-Type" />
2: <iframe src="child.htm"></iframe>
child.htm:
1: <meta content="text/html; charset=gb2312" http-equiv="Content-Type" />
2: <script language=javascript>1:2: function iframeAutoFit()3: {4: try5: {6: if(window!=parent)7: {8: var a = parent.document.getElementsByTagName("IFRAME");9: for(var i=0; i<a.length; i++)10: {11: if(a[i].contentWindow==window)12: {13: var h = document.body.scrollHeight;14: if(document.all) {h += 4;}15: if(window.opera) {h += 1;}16: a[i].style.height = h;17: }18: }19: }20: }21: catch (ex)22: {23: alert("脚本无法跨域操作!");24: }25: }26: if(document.attachEvent) window.attachEvent("onload", iframeAutoFit);27: else window.addEventListener('load', iframeAutoFit, false);28:29: </script>
30: <div style="width: 200px; height: 400px; background-color: yellow"> 31: iframe 自适应其加载的网页(多浏览器兼容) 32: </div>
