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,跨域拒絕訪問