The IFRAME adaptive height itself is a very simple method, that is, after the page load is completed, recalculate the height can be.
The code is as follows:
Copy Code code as follows:
Public method: Set the height of the IFRAME to ensure that all data is displayed
function Setpageheight () {
var iframe = geturlparam (' ifname ');
var myiframe = window.parent.document.getElementById (iframe);
Iframeloaded (Myiframe);
//}
var iframeloaded = function (iframe) {
if (Iframe.src.length > 0) {
if (!iframe.readystate | | iframe.readystate = = "complete") {
var bheight =
Iframe.contentWindow.document.body.scrollHeight;
var dheight =
Iframe.contentWindow.document.documentElement.scrollHeight;
var height = Math.max (bheight, dheight);
Iframe.height = height;
}
}
}
Reset the iframe height when paging; modified: Iframe.name = Iframe.id
var resetiframeheight = function ()
{
try {
var oiframe = parent.document.getElementById (window.name);
Oiframe.height = 100;
Iframeloaded (Oiframe);
}
catch (Err)
{
try {
Parent.document.getElementById (window.name). height = 1000;
\ catch (ERR2) {}
}
}
Call the Resetiframeheight () method.
But another situation is using jquery Ajax request data, the data is in the body load after the completion of the HTTP request, when no data occupy the window height, Resetiframeheight method can not calculate the height.
At this point, we think of a method: When Ajax can be done, of course, the complete event is performed.
But we also can't add processing to the Ajax complete events on every page. The global variables for jquery Ajax are used here.
Handling Ajax and IFRAME Adaptive code:
Copy Code code as follows:
var sendcount = 0;
var completecount = 0;
Add Ajax global event handling.
Resetiframeheight ();
$ (document). Ajaxstart (function (A, B, c) {
}). ajaxsend (function (E, XHR, opts) {
sendcount++;
}). ajaxerror (function (E, XHR, opts) {
}). ajaxsuccess (function (E, XHR, opts) {
}). ajaxcomplete (function (E, XHR, opts) {
completecount++;
Resetiframeheight ();
}). Ajaxstop (function () {
});
Execute the resetiframeheight first, then call Resetiframeheight after each Ajax completes.
Tested effectively.