JS iframe cross-domain access (same as primary/non-identical primary domain) in-depth introduction of _javascript skills

Source: Internet
Author: User
Tags gettext
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

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.