IFrame contains pages may be very large, need a very long load time, for this direct calculation of height, it is likely that the page has not finished downloading, height calculation will have problems. So it is best to compute the altitude in the onload event of the IFRAME. Also note here is that IE must use the Microsoft Event model Obj.attachevent to bind the onload event. Other browsers can also directly obj.onload = function () {}.
The 111cn.net code is as follows:
(function () {
var frame = document.getElementById ("Frame_content_parent"),
Setiframeheight = function () {
var framecontent = frame.contentwindow.document,
Frameheight = Math.max (framecontent.body.scrollheight,framecontent.documentelement.scrollheight);
Frame.height = Frameheight;
};
if (Frame.addeventlistener) {
Frame.addeventlistener ("Load", setiframeheight,false);
}else{
Frame.attachevent ("onload", setiframeheight);
}
})();
Same domain, sub page height will increase dynamically
Principle and the first case, more than one timer, has been testing the height of the word page, when the height of the sub page and the height of the IFRAME is inconsistent, reset the height of the iframe. Here you can also add a try to add a sufficient height when the page effect goes wrong.
The 111cn.net code is as follows:
(function () {
var _resetiframe = function () {
var frame = document.getElementById ("Frame_content_parent")
try {
var framecontent = frame.contentwindow.document,
Bodyheight = Math.max (framecontent.body.scrollheight,framecontent.documentelement.scrollheight);
if (bodyheight!= frame.height) {
Frame.height = Bodyheight;
}
}
catch (ex) {
frame.height = 1800;
}
}
if (Frame.addeventlistener) {
Frame.addeventlistener ("Load", function () {setinterval (_resetiframe,200);},false);
}else{
Frame.attachevent ("onload", function () {setinterval (_resetiframe,200);});
}
})();