JS Cross-domain is a topic of much discussion. Cross-domain access to IFrame is also well researched.
generally divided into two situations:
One, is the same as the main domain, the different subdomains between the Cross-domain;
With the main domain, different subdomains across domains, set the same Document.domian can be resolved;
The parent page accesses the child page and can be document.getElementById ("MyFrame"). Contentwindow.document to access the contents of the IFRAME page, or direct document.getElementById ("MyFrame") if the contentdocument is supported. Contentdocument access to the content of the child page;
Child pages to access the parent page, you can parent.js global properties
Two, is different main domain cross domain;
Premise, under the Www.a.com a.html,a.html iframe called under the www.b.com under the b.html,b.html under the IFRAME called www.a.com under the c.html
B.html is an object that does not have direct access to a.html, because it involves cross-domain, but can access the parent, and the same c.html parent can access b.html. C.html and a.html are in the same domain and can access objects under a. Parent.parent.js Object!
Look at the example below:
A.html
Copy Code code as follows:
<!doctype html>
<meta charset= "Utf-8" >
<title> Untitled Document </title>
<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>
B.html
Copy Code code as follows:
<!doctype html>
<meta charset= "Utf-8" >
<title> Untitled Document </title>
<body>
<iframe id= "Myfarme" src= "###" ></iframe>
<ul id= "CT" >
<li> here is the content 1</li>
<li> here is the content 2</li>
<li> here is the content 3</li>
<li> here is the content 4</li>
<li> here is the content 5</li>
<li> here is the 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>
C.html
Copy Code code as follows:
<!doctype html>
<meta Cha rset= "Utf-8" >
<title> Untitled document </title>
<script>
window.onload = function () {
var t ext = window.location.href.split (' = ') [1]
Console.log (parent.parent)
Parent.parent.dosome (text);
}
</script>
<body>
ddddddddddd
</body>
</HTML&G T