Supports dynamic zooming of cross-origin iframe height with content

Source: Internet
Author: User

The highly adaptive feature of iframe is a common issue. Today I will find a few more questions about the highly adaptive feature of iframe. I have sorted it out and shared it with my friends. The code is concise and compatible.

The iframe height is adaptive to the page content. When the page height changes, the iframe height also automatically changes.
Browser compatibility: IE6 ++, Firefox, Chrome, and other browsers are not tested. We look forward to your test comments to improve this article. Thank you)

Implementation in the same domain environment:
Method 1: Modify only the iframe parent page (iframeA. php). The iframe subpage content (iframeB. php) does not need to add other js or additional code.

Add the following code to the iframe parent page (iframeA. php:

The Code is as follows: Copy code

<Iframe width = "100%" height = "0" id = "frame_content" src = "/iframeB. php "scrolling =" no "frameborder =" 0 "onload =" this.height?this.content=#document.doc umentElement. scrollHeight "> </iframe>

<Script type = "text/javascript">
Function reinitIframe (){
Var iframe = document. getElementById ("frame_content ");
Try {
Var bHeight = iframe.content20.doc ument. body. scrollHeight;
Var dHeight = iframe.content?document.doc umentElement. scrollHeight;
Var height = Math. max (bHeight, dHeight );
Iframe. height = height;
} Catch (ex ){}
}
Window. setInterval ("reinitIframe ()", 200 );
</Script>

Method 2: modify the content of the iframe subpage (iframeB. php). The iframe parent page (iframeA. php) does not need to be added with js Code.
Add the following code to the iframe parent page (iframeA. php:

The Code is as follows: Copy code

<IFRAME border = 0 marginWidth = 0 frameSpacing = 0 marginHeight = 0 src = "/iframeB. php "frameBorder = 0 noResize scrolling =" no "width = 100% height = 100% vspale =" 0 "id =" childFrame "> </IFRAME>
The iframe subpage (iframeB. php) code example is as follows:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "zh-cn">
<Head>
<Script language = "javascript" type = "text/javascript">
Function IFrameResize (){
// Alert(this.doc ument. body. scrollHeight); // The height of the current page is displayed.
Var obj = parent.doc ument. getElementById ("childFrame"); // get the IFrame object of the parent page
// Alert (obj. height); // The height set in IFrame on the parent page is displayed.
Obj. height = this.doc ument. body. scrollHeight; // adjust the IFrame height in the parent page to the height of this page.
}
Window. setInterval ("IFrameResize ()", 200); // The time here can be set to be shorter. The shorter the time, the less obvious the jitter when the height changes.
</Script>
</Head>
<Body onload = "IFrameResize ()">
<Div id = 'main' style = 'background: # FFF; width: 100%; '>
Test Data
<Br/>
Test Data
<Br/>
Test Data
<Br/>

</Div>
</Div>
</Body>
</Html>

Note: The body onload attribute must be defined here.


Cross-origin Environment Implementation:
If iframe is cross-origin, it cannot be directly controlled by JS. It can only be controlled by an intermediate proxy. Here we select the iframe subpage (iframeB. php) to add a parent page with iframe (iframeA. php) same domain page (iframeC. php); in this way, the page iframeC. php and the parent page iframeA. php makes communication accessible. Because the sub-page iframeB. php page embedded iframeC. php, so the page iframeB. php can rewrite the page iframeC. the href value of php. Here is an example. Assume that the file corresponding to the domain name is as follows:

Reference
IframeA. php is located on the domain
IframeB. php is located on the domain
IframeC. php is located on the domain

Implementation Method:
IframeA. php code:

The Code is as follows: Copy code

<Iframe id = "ifr" src = "/iframeB. php" height = "200" width = "400" scrolling = "no" frameborder = "0"> </iframe>
<Script type = "text/javascript">
Var ifr_el = document. getElementById ("ifr ");
Function getIfrData (data ){
Ifr_el.style.height = data + "px ";
}
</Script>

IframeB. php code:

The Code is as follows: Copy code

<Style type = "text/css">
Body {
Background: # eee;
}
H1 {
Padding: 0;
Margin: 0;
Font-size: 16px;
}
H2 {
Padding: 0;
Font-size: 13px;
Font-weight: normal;
}
Iframe {
Visibility: hidden;
}
# Box {
Height: 220px;
}
</Style>
<Div id = "box">
<Button id = "btn_auto" type = "button"> Height Auto: off </button>
<Button id = "btn_plus10" type = "button"> Height + 10px </button>
<Button id = "btn_minus10" type = "button"> Height-10px </button>
</Div>
<Iframe id = "ifr" src = "/iframeC. php" width = "0" height = "0"> </iframe>
<Script type = "text/javascript">
Var box_el = document. getElementById ("box "),
Btn_auto_el = document. getElementById ("btn_auto "),
Btn_plus10_el = document. getElementById ("btn_plus10 "),
Btn_minus10_el = document. getElementById ("btn_minus10 "),
Ifr_el = document. getElementById ("ifr ");

Var isAuto = false,
OldHeight = 0,
IfrSrc = ifr_el.src.split ("#") [0];

Btn_auto_el.onclick = function (){
If (! IsAuto ){
IsAuto = true;
Btn_auto_el.innerHTML = "Height Auto: on ";
} Else {
IsAuto = false;
Btn_auto_el.innerHTML = "Height Auto: off ";
}
}
Btn_plus10_el.onclick = function (){
Var height = box_el.offsetHeight;
Box_el.style.height = (10 + height) + "px ";
}
Btn_minus10_el.onclick = function (){
Var height = box_el.offsetHeight;
Box_el.style.height = (height-10) + "px ";
}
SetInterval (function (){
If (isAuto ){
Var height = document. body. scrollHeight;
Height + = 20;
If (oldHeight! = Height ){
OldHeight = height;
Ifr_el.src = ifrSrc + "#" + oldHeight;
}
}
},200 );
</Script>

IframeC. php code:

The Code is as follows: Copy code

<Script type = "text/javascript">
Var oldHeight = 0;

SetInterval (function (){
Var height = location. href. split ("#") [1];
If (height & oldHeight! = Height ){
OldHeight = height;
If (window. parent. parent. getIfrData ){
Window. parent. parent. getIfrData (oldHeight );
}
}
},200 );
</Script>

Note: Do not set the height of the div containing the iframe, and do not set the height of the iframe.

Note: submit of the iframe subpage submits the jump process.
<Form name = "myform" id = "myform" method = "post" action = http://www.bKjia. c0m target = "_ self"> </form>
Note: target = "_ self"

Related Article

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.