Source: http://www.javaeye.com/topic/644044
Problem description
Today, when dealing with an IFRAME adaptive height, a multi-layer IFRAME reference cannot support the height. The effect is the same as demo1.
Cause Analysis
H3.html contains H3. h3.html content, which may cause high adaptability. The general solution is to add the onload event to reference the IFRAME of H3 to make the IFRAME highly adaptive reference the page height. In this case, we open h1.html and the development page is still not supported. This is because the page is loaded at the first level. When H1 references H2, the height of H2 is fixed, when H3 is loaded, the height of H2 is changed, but the reference page height of H1 is not changed, so the page still does not seem to be supported, after discussing with colleagues, I wrote a javascript code to automatically recalculate the height of the IFRAME element in the parent (parent page) to solve this problem, the principle is to obtain the IFRAME element of the parent on the page through a loop. It ends when the top layer is obtained.
Adaptation to a single IFRAME
Onload="This.style.height=iframe1.doc ument. Body. scrollheight"
Note: iframe1 is the ID of the framework. I am trying an adaptive method without hard writing the name. If it is successful, replace it.
Adaptive Multilevel IFRAME height
// Enable IFRAME automatically, so that the IFRAME of all parent pages automatically adapts to the page height.
Function Autoheight (){
VaR Doc = Document,
P = Window;
While (P = P. Parent ){
VaR Frames = P. frames,
Frame,
I = 0 ;
While (Frame = Frames [I ++ ]) {
If (Frame.doc ument = Doc ){
Frame. frameelement. style. Height = Doc. Body. scrollheight;
Doc = P.doc ument;
Break ;
}
}
If (P = Top ){
Break ;
}
}
}
Comments from a netizen:
Post a method we used before, which is easier to use than others:
VaR Getffversion = Navigator. useragent. substring (navigator. useragent. indexof ( " Firefox " ). Split ( " / " )[ 1 ]
VaR Ffextraheight = Getffversion > = 0.1 ? 16 : 0
Function Dyniframesize (iframename ){
VaR Ptar = Null ;
If (Document. getelementbyid ){
Ptar = Parent.doc ument. getelementbyid (iframename );
}
Else {
Eval ( ' Ptar = ' + Iframename + ' ; ' );
}
If (Ptar && ! Window. Opera ){
If (Ptar. contentdocument && Ptar. contentdocument. Body. offsetheight ){
Ptar. style. Height = Ptar. contentdocument. Body. offsetheight + Ffextraheight + 25 ;
}
Else If (Ptar. Document && Ptar. Document. Body. scrollheight ){
Ptar. style. Height = Ptar. Document. Body. scrollheight;
}
}
}