In-depth introduction to js iframe cross-origin access (same main domain/non-main domain)

Source: Internet
Author: User

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>

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.