Google Chrome is very convenient in debugging JavaScript scripts. Today, when using Google Chrome to debug code, I accidentally found a strange problem: blocked a frame with origin "null" from accessing a frame with origin "null ". protocols, domains, and ports must match ., this is mainly because the parent of the page where the iframe is located is not obtained. The Code is as follows:
Index.html:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Script type = "text/javascript">
Function test (){
Alert ('you have successfully called the function on the page where the iframe tag is located! ');
}
</Script>
</Head>
<Body>
<Iframe frameborder = "1" srcw.'child.html 'style = "width: 50%; height: 50%;"> </iframe>
<Div style = "font-size: 36px; margin-left: 40%; color: red;"> iframe tag page </div>
</Body>
</Html>
Child.html:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
</Head>
<Body>
<Input type = "button" onclick = "javascript: window. parent. test ();" value = "Call the function on the page where the iframe tag is located"/>
<Div style = "font-size: 21px; margin-left: 40%; color: red;"> pages referenced by the iframe tag </div>
</Body>
</Html>
[Download code file]
If the above Code runs directly on the computer using Google Chrome, the above error "Blocked a frame with origin" null "from accessing a frame with origin" null "will occur ". this is not the case for Protocols, domains, and ports must tables files.
Summary: if you use the file protocol locally to open a page containing the iframe tag in Google browser, the page referenced by the iframe tag cannot access the parent object on the page where the iframe tag is located, unless published on a server (such as Tomcat.