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: