iframe 页面自适应长宽
脚本:
View Code
// 调用 IFrameReSize("mainFrame");IFrameReSizeWidth("mainFrame"); //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; } } }
页面调用:
View Code
<iframe name="paymentIframe" id="paymentIframe" onload='IFrameReSize("paymentIframe");IFrameReSizeWidth("paymentIframe");' src="/docc/blank.jsp" align="center" allowtransparency="true" scrolling="auto" width="100%" height="100%" frameborder="0" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" noresize></iframe>
在新增或修改iframe信息后的js中也需要重新设置IFrame的长宽。
IFrameReSize("paymentIframe");IFrameReSizeWidth("paymentIframe");