当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 让iframe自适应高度(支持XHTML,支持FF)

Javascript
innertext , insertadjacentelement , insertadjacenthtml , insertadjacenttext 等区别
JavaScript与C# Windows应用程序交互方法
javascript之解决IE下不渲染的bug
javascript之锁定表格栏位
javascript实现鼠标选取拖动或Ctrl选取拖动
优化网页之快速的呈现我们的网页
javascritp实现input输入框相关限制用法
用javascript实现的激活输入框后隐藏初始内容
CSS代码格式化和压缩的方法与技巧
autocomplete禁止自动完成功能
IE autocomplete internet explorer''s autocomplete
如何快速的呈现我们的网页的技巧整理
计算黄金分割的javascript代码
设置和读取cookie的javascript代码
javascript 复选框选择/全选后特效
实现一个年、月、季度联动SELECT的javascript代码
javascript事件模型代码
WordPress 插件——CoolCode使用方法与下载
用javascript实现画图效果的代码
javascript发表评论或者留言时的展开效果

Javascript 中的 让iframe自适应高度(支持XHTML,支持FF)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 61 ::
收藏到网摘: n/a

先说明,这个办法只限于iframe中的子页面也是本地页面(不能引用外网页面)
======方法=====
第一步 js部分
function getSize() {
var xScroll, yScroll;
if (window.innerHeight && window.scrollMaxY) {
xScroll = document.body.scrollWidth;
yScroll = window.innerHeight + window.scrollMaxY;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
xScroll = document.body.scrollWidth;
yScroll = document.body.scrollHeight;
} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
xScroll = document.body.offsetWidth;
yScroll = document.body.offsetHeight;
}
var windowWidth, windowHeight;
if (self.innerHeight) { // all except Explorer
windowWidth = self.innerWidth;
windowHeight = self.innerHeight;
} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
windowWidth = document.documentElement.clientWidth;
windowHeight = document.documentElement.clientHeight;
} else if (document.body) { // other Explorers
windowWidth = document.body.clientWidth;
windowHeight = document.body.clientHeight;
}
// for small pages with total height less then height of the viewport
if(yScroll < windowHeight){
pageHeight = windowHeight;
y = pageHeight;
} else {
pageHeight = yScroll;
y = pageHeight;
}
if(xScroll < windowWidth){
pageWidth = windowWidth;
} else {
pageWidth = xScroll;
}
arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
return arrayPageSize;
}
这段代码用来获取目标页的参数,包括页面高、宽,屏幕高、宽
function autoHeight(pid) {
var x = new getSize();
parent.document.getElementById(pid).height=x[1];
}
这段代码用来实现父页面中iframe的高度自适应
\\\\\\\\\\
第二步 页面部分
<div class="onright" style="width:480px;"><iframe id="infrm" name="infrm" marginwidth="0" marginheight="0" width="100%" src="park.htm" frameborder="0" scrolling="auto"></iframe>
这是父页面的iframe,没什么特别的,和普通的iframe一样,不过要设置好id值,以便子页面的参数调用。
<body onload="autoHeight('infrm')"></body>
在body中利用onload事件,将自身的高度传给父页面的iframe。