IE8,11的iframe高度自适应

兼容模式:
function iFrameHeightTzinfo() {
 var ifm= document.getElementById("iframe_tzinfo");

 //var subWeb = document.frames ? document.frames["iframe_tzinfo"].document : ifm.contentDocument; 
//document.frames["iframe_tzinfo"].document 兼容模式下在同域名下有效,跨域下拒绝访问
//ifm.document兼容模式下在IE跨域和同域下均有效,因此优先采用getElementById的方式来取得document

 var subWeb = document.frames ? ifm.document : ifm.contentDocument;  
 if(ifm != null && subWeb != null) {
    ifm.height = subWeb.body.scrollHeight+10 || 255;
   ifm.height=600
 }
 
 //下面的代码和上方取对象相同,都是从ifm.document取高度
  //if (ifm.contentDocument && ifm.contentDocument.body.offsetHeight){//IE11
    // ifm.height = ifm.contentDocument.body.offsetHeight;
    // }else if(ifm.document && ifm.document.body.scrollHeight){ //IE8
    //   ifm.height = ifm.document.body.scrollHeight;
 //}
}

...
<iframe id="iframe_tzinfo" width="100%"  frameBorder=0  onLoad='iFrameHeightTzinfo();'  src="http://128.8.18.118:9080/test1.jsp"></iframe>

非兼容模式下:
document.getElementById("iframe_tzinfo").document  //IE8,IE11同域跨域undefind;
document.getElementById("iframe_tzinfo").contentDocument  //IE8,IE11同域可用,跨域拒绝访问
document.frames["iframe_tzinfo"].document  //IE11,IE8同域可用,跨域拒绝访问,
document.frames["iframe_tzinfo"].contentDocument  //IE8,IE11同域undefind,跨域拒绝访问

兼容模式下:
document.getElementById("iframe_tzinfo").document  //IE8,11同域,跨域都可用------------->推荐
document.getElementById("iframe_tzinfo").contentDocument  //IE8,11同域跨域undefind
document.frames["iframe_tzinfo"].document //IE8,11同域可用,跨域拒绝访问
document.frames["iframe_tzinfo"].contentDocument//IE8,11,同域undefind,跨域拒绝访问

猜你喜欢

转载自www.cnblogs.com/tapt/p/9908930.html
今日推荐