您的位置:首页 > 其它

设置iframe自适应高度(/*兼容FF/IE9/IE8/IE7/IE6*)

2011-10-19 16:18 531 查看
js:

function iFrameHeight() {

var ifm= document.getElementById("iframepage");

var subWeb = document.frames ? document.frames["iframepage"].document : ifm.contentDocument;

if(ifm != null && subWeb != null) {

ifm.height = subWeb.body.scrollHeight;

}

}

iframe:

<iframe id="iframepage" src="study_1.asp" frameborder="0" scrolling="no" style="width:100%; " onLoad="iFrameHeight()"></iframe>

下面的兼容性比较好:

/*兼容FF/IE9/IE8/IE7/IE6*/

function iframeResize(iframe) {

try {

//var iframe = document.getElementById("contentFrame"); //("contentFrame");

var idocumentElement = iframe.contentWindow.document.documentElement;

if (idocumentElement.scrollHeight > 560) {

iframe.height -= 5;

iframe.height = idocumentElement.scrollHeight;

}

else {

iframe.height = 560;

}

}

catch (e) {

window.status = 'Error: ' + e.number + '; ' + e.description;

}

}

iframe:

<iframe id="iframepage" src="study_1.asp" frameborder="0" scrolling="no" style="width:100%; " onLoad="iframeResize(this)"></iframe>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: