Home >Web Front-end >JS Tutorial >js iframe cross-domain access (same main domain/non-same main domain) are introduced in depth_javascript skills

js iframe cross-domain access (same main domain/non-same main domain) are introduced in depth_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:43:012096browse

js cross-domain is a topic that has been discussed a lot. iframe cross-domain access has also been thoroughly studied.
Generally divided into two situations:
1. Cross-domain between different sub-domains under the same main domain;
Same main domain but different sub-domains Cross-domain issues can be solved by setting the same document.domian;
The parent page can access the child page by document.getElementById("myframe").contentWindow.document to access the content of the iframe page; if contentDocument is supported, the document can also be directly accessed .getElementById("myframe").contentDocument accesses the content of the sub-page;
The sub-page accesses the parent page, and can use the parent.js global attribute
2. Cross-domain between different main domains ;
The premise is that a.html under www.a.com, the iframe in a.html calls b.html under www.b.com, and the iframe under b.html calls c.html under www.a.com
b.html is not an object that cannot directly access a.html because it involves cross-domain, but it can access parent. Similarly, the parent of c.html 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 following example:
 a.html

Copy code The code is as follows:





Untitled document




  • Here is content 1
  • Here is content 2

  • Here is content 3

  • Here is content 4

  • Here is content 5

  • Here is content 6


<script> <br>window.onload = function (){ <br>var text = document.getElementById('ct').innerHTML; <br>document.getElementById('myfarme').src="http://www.a.com/c.html?content =" encodeURI(text); <br>} <br></script>



c.html
Copy code The code is as follows:





Untitled Document
<script> <br>window.onload = function (){ <br>var text = window.location.href.split('=')[1] <br>console.log(parent.parent) <br>parent.parent.dosome(text); <br> } <br></script>


ddddddddddd


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn