[JS]-IFRAME adaptive height, unlimited parent framework, multi-layer framework adaptive

Source: Internet
Author: User

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;
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.