<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>iframe自适应大小</title>
<script type="text/javascript">
function reinitIframe(){
iframe = document.getElementById("frame1");
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
try{
//var height = window.document.body.scrollHeight;
//var width = window.document.body.scrollWidth;
var height = window.document.body.parentElement.clientHeight;
var width = window.document.body.parentElement.clientWidth;
//var bHeight = iframe.ownerDocument.body.scrollHeight;
// var dHeight = iframe.ownerDocument.documentElement.scrollHeight;
// if(i%2==0)
// alert("body=="+bHeight+"doc=="+dHeight+"a=="+a);
// var height = Math.min(bHeight, dHeight);
if(iframe.height != (height*0.95) && (height*0.95) > 300 ){
iframe.height = height*0.95;
h = height;
}
if(iframe.width != (width*0.96) (width*0.96) > 300){
iframe.width = width*0.96;
w = width;
}
}catch (ex){}
}
window.setInterval("reinitIframe()", 1000);
</script>
</head>
<body>
<iframe id="frame1" src="1.html" width="640" scrolling="no" frameborder="0" ></iframe>
</body>
</html>
js 让iframe自适应大小;测试IE8,火狐,Chrome可运行 .
最新推荐文章于 2020-12-24 18:45:07 发布