Ifram framework adaptive content height (code is easy to understand ie7, 8, 9, Firefox, Google all tested)

Source: Internet
Author: User

The height of the Self-adaptive content in the Ifram framework is a problem that has plagued me for a long time. Today I have time to take out the previous Code and study it. Recently I am working on a project, I often use the JQ method. I found the method:

 


This was previously found online and only supports IE7: the code is as follows:

<Script language = "javascript">
Function SetCwinHeight () // auto-stretch
{
Var xsun = document. getElementById ("main"); // iframe id
If (document. getElementById ){
If (xsun &&! Window. opera ){
If (xsun. contentDocument & xsun. contentDocument. body. offsetHeight ){
Xsun. height = xsun. contentDocument. body. offsetHeight + 20;
} Else if (xsun. Document & xsun. Document. body. scrollHeight ){
Xsun. height = xsun. contentDocument. body. offsetHeight + 20;
}
}
}
}
</Script>

 


The modified code:

<Script language = "javascript">
Function SetCwinHeight () // auto-stretch
{
Var xsun = document. getElementById ("main"); // iframe id
// Var h1 = xsun. contentDocument. body. offsetHeight;
Var h2 = document.doc umentElement. offsetHeight;
If (document. getElementById ){
If (xsun &&! Window. opera ){
If (xsun. contentDocument & h2 ){
Xsun. height = h2 + 20;
} Else if (xsun. Document & xsun. Document. body. scrollHeight ){
Xsun. height = h2 + 20;
}
}
}
}
</Script>

We should be able to clearly see the difference, that is, var h1 = xsun. contentDocument. body. offsetHeight;
Var h2 = document.doc umentElement. offsetHeight;

The obtained height from h1 is 0, and h2 is the correct height. What is the specific meaning? I will know the difference between the two when I go to Baidu.

Call method:

<Iframe frameborder = "0" id = "main" name = "main" onload = "javascript: SetCwinHeight ();"
Src = "xsun.html" marginwidth = "0" marginheight = "0" scrolling = "no" style = "z-index: 1;
Width: 100%; min-height: 300px; "> </iframe>

 

 

 

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.