Javascript cross-origin problem-Cross-origin iframe adaptive size implementation code _ javascript skills

Source: Internet
Author: User
A few days ago, when I was working on a cooperative project between kaixinnet and kaixinnet, I encountered the problem of iframe cross-domain self-adaptation. At the beginning, I was confused that kaixin.com's technical engineers sent me a piece of code like this. The Code is as follows:



Script
Function setHeight (){
Var dHeight = document.doc umentElement. scrollHeight;
Var t = document. createElement ("p ");
T. innerHTML = '';
Document.doc umentElement. appendChild (t. firstChild );
}
Script


I want to solve cross-origin problems! Then I went to the Internet to search and suddenly realized that

The details are as follows:


Problem:

The page a.htmunder the domain name is inserted into the page B .html under the B domain name through iframe. the size of B .html is unpredictable and will change. Therefore, the iframe adaptive size in a.htm is required.

Problem nature:

For cross-domain question of js, to control iframein a.htm, the size of B .html is obtained first. A and B do not belong to the same domain. js access is restricted and B .html cannot be read.

Solution:

B is A cooperative relationship, and B .html can introduce the js provided by.

First, a.html introduced B .html through iframe.

The Code is as follows:





Bintroduces the js file provided by A in B .html.

Html code

The Code is as follows:


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.