Dynamically changing the height of IFrame to realize automatic stretching of IFRAME to adapt to high _javascript skill

Source: Internet
Author: User

Dynamic change of the height of the IFRAME, to achieve the automatic extension of the IFRAME, the parent page is also automatic God shrink
Principle: When the IFRAME page is loaded, call the parent IFrame object and change its height
To achieve a specific :
1, in the specific page of the IFRAME (that is, the sub page), add JavaScript

Copy Code code as follows:

<script>
function Iframeresize () {
alert (this.document.body.scrollHeight); Eject the height of the current page
var obj = parent.document.getElementById ("Childframe"); Get the parent page IFrame Object
alert (obj.height); Eject the height set in the IFRAME in the parent page
Obj.height = This.document.body.scrollHeight; Adjust the height of the iframe in the parent page for this page
}
</script>

2, in the specific page of the IFRAME (that is, the child page) in the body, add onload event
Copy Code code as follows:

<body onload= "iframeresize ()" >

3. Add ID for the IFRAME tag of the parent page, that is, the first step above, the Childframe written in the 2nd line of the method body
Copy Code code as follows:

<iframe border=0 marginwidth=0
Framespacing=0 marginheight=0
Src= "frame1.jsp" frameborder=0
Noresize scrolling= "No" width=100% height=100% vspale= "0" id= "childframe" ></IFRAME>

Concrete Implementation Two:
Copy Code code as follows:

Dynamically changing the height of an iframe in a parent class
The IFRAME page calls the JS
$ (function () {
To the height of the window
var Winh = $ (window). Height ();
Take the height of the page
var bodyh = $ (document). Height ();
if (Bodyh > Winh) {
Window.parent.document.getElementById ("MainFrame"). Height=bodyh;
}else{
Window.parent.document.getElementById ("MainFrame"). Height=winh;
}
});

The iframe of the parent page is
Copy Code code as follows:

<iframe src= "Main.action" height= "M" frameborder= "0" width= "100%" id= "MainFrame" name= "MainFrame" ></ Iframe>

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.