关闭

iframe 自动适应子页面高度宽度

标签: iframe 多个浏览器兼容
631人阅读 评论(0) 收藏 举报
分类:

<iframe id="mainFrame" name="mainFrame" scrolling="no" src="Index.aspx"
            frameborder="0" style="padding: 0px; width: 100%; height: 1000px;"></iframe>


<script type="text/javascript">

        startInit('mainFrame', 560);

var browserVersion = window.navigator.userAgent.toUpperCase();

var isOpera = browserVersion.indexOf("OPERA") > -1 ? true : false;

var isFireFox = browserVersion.indexOf("FIREFOX") > -1 ? true : false;

var isChrome = browserVersion.indexOf("CHROME") > -1 ? true : false;

var isSafari = browserVersion.indexOf("SAFARI") > -1 ? true : false;

var isIE = (!!window.ActiveXObject || "ActiveXObject" in window);

var isIE9More = (! -[1, ] == false);

function reinitIframe(iframeId, minHeight) {

try {

var iframe = document.getElementById(iframeId);

var bHeight = 0;

if (isChrome == false && isSafari == false)

bHeight = iframe.contentWindow.document.body.scrollHeight;


var dHeight = 0;

if (isFireFox == true)

dHeight = iframe.contentWindow.document.documentElement.offsetHeight + 2;

else if (isIE == false && isOpera == false)

dHeight = iframe.contentWindow.document.documentElement.scrollHeight;

else if (isIE == true && isIE9More) {//ie9+

var heightDeviation = bHeight - eval("window.IE9MoreRealHeight" + iframeId);

if (heightDeviation == 0) {

bHeight += 3;

} else if (heightDeviation != 3) {

eval("window.IE9MoreRealHeight" + iframeId + "=" + bHeight);

bHeight += 3;

}

}

else//ie[6-8]、OPERA

bHeight += 3;


var height = Math.max(bHeight, dHeight);

if (height < minHeight) height = minHeight;

iframe.style.height = height + "px";

} catch (ex) { }

}

function startInit(iframeId, minHeight) {

eval("window.IE9MoreRealHeight" + iframeId + "=0");

window.setInterval("reinitIframe('" + iframeId + "'," + minHeight + ")", 100);

}

</script>



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11714次
    • 积分:396
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条