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.