Javascript-based real iframe highly adaptive (compatible with IE, FF, and Opera) _ javascript skills

Source: Internet
Author: User
To meet project requirements, I used an iframe highly adaptive function. I searched google for a long time and found some modifications. You can test it. Find the following js

The Code is as follows:


Function SetCwinHeight (obj)
{
Var cwin = obj;
If (document. getElementById)
{
If (cwin &&! Window. opera)
{
If (cwin. contentDocument & cwin. contentDocument. body. offsetHeight)
Cwin. height = cwin. contentDocument. body. offsetHeight + 20;
Else if (cwin. Document & cwin. Document. body. scrollHeight)
Cwin. height = cwin. Document. body. scrollHeight + 10;
}
}
}


Then ......
The test process is started (the call is simple and skipped first)
1. IE --- there is a slight gap between the height and the size of the scroll bar.
2. FF --- similar to IE, there is a small gap
3. Opera --- you can see the JS conditions.
However, mainstream browsers must use at least these three items !!!
So Google
Search for special phenomena that browsers encounter when processing document. scrollHeight or offsetHeigth
It is found that when Opera Browser processes iframe content, it uses contentWindow
However, when the processing content is high, it is consistent with IE.
Thus, with the following section of js

The Code is as follows:




Script
Function SetCwinHeight (obj)
{
Var cwin = obj;
If (document. getElementById)
{
If (cwin &&! Window. opera)
{
If (cwin. contentDocument & cwin. contentDocument. body. offsetHeight)
Cwin. height = cwin. contentDocument. body. offsetHeight + 20; // FF NS
Else if (cwin. Document & cwin. Document. body. scrollHeight)
Cwin. height = cwin. Document. body. scrollHeight + 10; // IE
}
Else
{
If(cwin.content+doc ument & cwin.content+doc ument. body. scrollHeight)
Cwin. height = cwin.content20.doc ument. body. scrollHeight; // Opera
}
}
}
Script


&lt;BR&gt; &lt;/body&gt; &lt;BR&gt; </ptml&gt; &lt;BR&gt; &lt;/p&gt; &lt;BR&gt;, finally, we adapted these three browsers. &lt;BR&gt; as a programmer, we still need to be careful. &lt;BR&gt; try again. &lt;BR&gt; OK... the three browsers are normally displayed, and there is no vertical scroll bar for 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.