Adapt to each browser's iframe height automatic adjustment

Source: Internet
Author: User

1, mzone.cc.js file, to create a namespace-based object

The code is as follows Copy Code

function Mzone () {};


/**

* Desc: Object creation using the given object path

* Para:objpath-Object path, String type, format: Xx.yy.zz

* Return: No value returned, use object path directly when creating success

*/


Mzone.createobject = function (/*string*/objpath) {

if (!objpath | | typeof (Objpath)!= ' string ') return;

var root = window;

var parts = objpath.split ('. ');

for (var i = 0; i < parts.length i + +) {

if (typeof (Root[parts[i]]) = = ' undefined ') root[parts[i]] = {};

root = Root[parts[i]];

}

};

2, Mzone.cc.iframe.js file, the automatic height of the iframe adjustment

* Title:iframe Height automatic adjustment, adapt to each browser (IE, Firefox, chrome, etc.). Note that you must test in the Web container when testing in chrome

The code is as follows Copy Code

Mzone.createobject ("Mzone.cc.iframe");

/**

* Desc: Define object Mzone.cc.iframe static method to realize dynamic adjustment of IFRAME

* Para:iframe-The IFrame object to adjust, Dom object

* Extheight-Automatically calculate the height of the iframe after an additional height value, digital

* Minimum height of minheight-iframe, digital type

* Return: No value returned

*/

Mzone.cc.iframe.autoHeight = function (/*object*/iframe,/*int*/extheight,/*int*/minheight) {

Iframe.style.display = "block";

if (iframe.contentdocument && iframe.contentDocument.body.offsetHeight) {//Ff,chrome etc

var h = parseint (iframe.contentDocument.body.offsetHeight) + 50;

H + + extheight;

if (H < minheight) H = minheight;

Iframe.height = h + "px";

else if (IFRAME. Document && iframe. Document.body.scrollHeight) {//IE

var h = parseint (iframe). Document.body.scrollHeight) + 35;

H + + extheight;

if (H < minheight) H = minheight;

Iframe.height = h + "px";

} else {

var h = parseint (iframe.contentWindow.document.documentElement.scrollHeight) + 35;

H + + extheight;

if (H < minheight) H = minheight;

Iframe.height = h + "px";

}

}


The above two JS is saved and introduced to the page can be called as follows to achieve the height of the iframe automatic adjustment:

The code is as follows Copy Code


var iframe = document.getElementById ("Iframe_body"); IFrame Object


var extheight = 0; The height value of the IFRAME to be added

var minheight = 300; Minimum height setting for IFRAME


Mzone.cc.iframe.autoHeight (IFRAME, Extheight, minheight);


The above scripts are tested in IE, Firefox, and Chrome, and you need to be aware that the offline test in Google's Chrome browser is faulty and needs to be tested in a Web container such as Apache or IIS to see the effect.

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.