Using jQuery to solve the problem of iframe adaptive height without determining the browser height _ jquery

Source: Internet
Author: User
This article mainly introduces how to use jQuery to solve the problem of iframe adaptive height without determining the browser height. If you need it, you can refer to the following two simple methods, you don't have to write anything to determine the browser height and width.

You can choose one of the following two methods. One is stored on the same page as iframe, and the other is stored on the test.html page.

Be sure not to put the error.
In the iframe Code, be sure to write the ID, no ID can be found

The Code is as follows:



Method 1:

The Code is as follows:


// Note: the following code is put on the same page as iframe for calling.
$ ("# Main"). load (function (){
Var mainheight = $ (this). contents (). find ("body"). height () + 30;
$ (This). height (mainheight );
});

Method 2:

The Code is as follows:


// Note: the following code is stored in test.html for calling.
Vertex (w.w.parent.doc ument). find ("# main"). load (function (){
Var main = warn (window.parent.doc ument). find ("# main ");
Var thisheight = $ (document). height () + 30;
Main. height (thisheight );
});

Iframe must be used during project creation, but iframe has a height by default. content beyond the default height will be hidden, the content smaller than the default height will regard the default height as the content height. In the process of searching for answers, I found out how to control iframe height self-adaptation.

Iframe adaptive height itself is a very simple method, that is, after the page is loaded, re-calculate the height.

The Code is as follows:

The Code is as follows:


// Public method: Set the iframe height to ensure that all data is displayed.
// Function SetPageHeight (){
// Var iframe = getUrlParam ('ifname ');
// Var myiframe = Response response parent.doc ument. getElementById (iframe );
// IframeLoaded (myiframe );
//}
Var iframeLoaded = function (iframe ){
If (iframe. src. length> 0 ){
If (! Iframe. readyState | iframe. readyState = "complete "){
Var bHeight =
Iframe.content+doc ument. body. scrollHeight;
Var dHeight =
Iframe.content##document.doc umentElement. scrollHeight;
Var height = Math. max (bHeight, dHeight );
Iframe. height = height;
}
}
}
// Reset the iframe height when paging; after modification: iframe. name = iframe. id
Var reSetIframeHeight = function ()
{
Try {
Var oIframe = parent.doc ument. getElementById (window. name );
OIframe. height = 100;
IframeLoaded (oIframe );
}
Catch (err)
{
Try {
Parent.doc ument. getElementById (screen. name). height = 1000;
} Catch (err2 ){}
}
}

Call the reSetIframeHeight (); method.

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.