Js cross-origin is a topic of discussion. Iframe cross-origin access is also thoroughly studied.
Generally, there are two situations::
1. Cross-Domain Communication between different subdomains under the same primary domain;
Set the same document. domian as the primary domain and different subdomains for cross-origin;
Access the child page on the parent page, which can be document. getElementById ("myframe" character .content?w.doc ument to access the content of the iframe page; If contentDocument is supported, you can directly document. getElementById ("myframe "). contentDocument;
The child page accesses the parent page, which can be a global attribute of parent. js.
2. Cross-origin of different primary Domains;
Bytes
B .html is the object of a.html. For cross-domain access, you can upload a parentd. For example, B .html can be used for a part of c.html. C.html and a.html are in the same domain and can access objects under. Parent. parent. js object!
See the following example:
A.html
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </title>
</Head>
<Body>
<Iframe src = "http://www. B .com/ B .html"> </iframe>
<Ul id = "getText"> </ul>
<Script>
Function dosome (text ){
Document. getElementById ("getText"). innerHTML = decodeURI (text );
}
</Script>
</Body>
</Html>
B .html
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </title>
</Head>
<Body>
<Iframe id = "myfarme" src = "###"> </iframe>
<Ul id = "ct">
<Li> content 1 </li>
<Li> content 2 </li>
<Li> content 3 </li>
<Li> content 4 </li>
<Li> content 5 </li>
<Li> content 6 </li>
</Ul>
<Script>
Window. onload = function (){
Var text = document. getElementById ('ct '). innerHTML;
Document. getElementById ('myfarme'). src = "http://www.a.com/c.html? Content = "+ encodeURI (text );
}
</Script>
</Body>
</Html>
C.html
Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> untitled document </title>
<Script>
Window. onload = function (){
Var text = window. location. href. split ('=') [1]
Console. log (parent. parent)
Parent. parent. dosome (text );
}
</Script>
</Head>
<Body>
Ddddddddddd
</Body>
</Html>