_javascript Techniques for solving the problem of IFRAME adaptive height in jquery ajax application

Source: Internet
Author: User
Tags http request

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.

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.