JS Dynamic acquisition of IFRAME sub-page Height summary

Source: Internet
Author: User

The cause of the problem

The product has a comment list refers to an IFRAME, the height is not fixed and thus triggered this summary.

Method 1: The parent page gets the height of the child page to set the height of the element

This method is used in the parent page to set the height of the iframe by getting the height of the child page

Some compatibility issues are involved:

IE with attachevent | 3C uses onload to determine whether a child page is loaded or not.

IE with Contentwindow | 3C using Contentdocument to get sub-pages

IE with Document.documentElement.scrollHeight (compatible with IE6 IE7) | 3C get page height with body.scrollheight

functionsetiframeheight (ID) {Try{        variframe =document.getElementById (ID); if(iframe.attachevent) {iframe.attachevent ("OnLoad",function() {iframe.height=Iframe.contentWindow.document.documentElement.scrollHeight;            }); return; }Else{iframe.onload=function() {iframe.height=Iframe.contentDocument.body.scrollHeight;            }; return; }         }Catch(e) {Throw NewError (' Setiframeheight error '); }}

Method 2: Child pages Set the height of the parent page element

This method is used in the child page to set the height of the parent iframe by getting the height of the child page

The child page gets the parent iframe by using parents to set the height for the IFRAME, compatible with Method 1.

The disadvantage is that the IFRAME has a cache when the parent page is brushed, and the demand cleanup cache takes effect.

functionsetparentiframeheight (ID) {Try{        varParentiframe =parent.document.getElementById (ID); if(window.attachevent) {window.attachevent ("OnLoad",function() {parentiframe.height=Document.documentElement.scrollHeight;            }); return; }Else{window.onload=function() {parentiframe.height=Document.body.scrollHeight;            }; return; }         }Catch(e) {Throw NewError (' Setparentiframeheight error '); }}

Cross-domain operations to be aware of

If there is one case for two pages, two subdomains:

Aaa.xxx.com
Bbb.xxx.com

You need to set up two pages as follows:

Document.domain = "xgo.com.cn";

This way the two pages will be able to manipulate each other. That is, the "cross-domain" between the same base domain is implemented.

Cross-domain implementation with Document.domain:
Prerequisites: These two domain names must belong to the same base domain name! And the protocol used, the port must be consistent, or you can not use Document.domain cross-domain

JavaScript prohibits two or more pages of different domains from interacting with one another for security reasons.
Pages in the same domain do not have any problems when working with each other.

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.