iframe自适应宽度和高度

//iframe高度自适应

function IFrameReSize(iframename) {

var pTar = document.getElementById(iframename);

if (pTar) { //ff

if (pTar.contentDocument && pTar.contentDocument.body.offsetHeight) {

pTar.height = pTar.contentDocument.body.offsetHeight;

} //ie

else if (pTar.Document && pTar.Document.body.scrollHeight) {

pTar.height = pTar.Document.body.scrollHeight;

}

}

}
//iframe宽度自适应

function IFrameReSizeWidth(iframename) {

var pTar = document.getElementById(iframename);

if (pTar) { //ff

if (pTar.contentDocument && pTar.contentDocument.body.offsetWidth) {

pTar.width = pTar.contentDocument.body.offsetWidth;

} //ie

else if (pTar.Document && pTar.Document.body.scrollWidth) {

pTar.width = pTar.Document.body.scrollWidth;

}

}

}
<iframe src="Main.htm" scrolling="no" frameborder="0" height="100%" id="mainFrame" width="100%" onload='IFrameReSize("mainFrame");IFrameReSizeWidth("mainFrame");'></iframe>
posted @ 2013-10-30 23:27  白小虫  阅读(2244)  评论(0编辑  收藏  举报