Can't Access Parent-document from Iframe

2019-06-05 02:56发布

问题:

I have a main page for navigating to other 3 pages in an iframe

Main page: 1.html

Other 3 pages: one.html, two.html, 3.html.

Code

1.html

<html>
    <body>
        <div>
           <ul>
             <li style="float:left; margin:20px;"><a href="One.html" target="target">One</a></li>
             <li style="float:left; margin:30px;"><a href="Two.html" target="target">Two</a></li>
             <li style="float:left; margin:50px;"><a href="Three.html" target="target">Three</a></li>
           </ul>
           <iframe id="target"></iframe>
        </div>
    </body>
</html>

one.html

<html>
    <body onload="try{alert(parent.location.href);} catch(e){alert(e);}">
        1.html.
    </body>
</html>

But there is a problem showing the location of the parent-document. It's giving an alert message as follws:

SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.

Can anyone please explain the meaning of the terms used in the message in the context of this error or simply why this happend???

N.B.: No Server Used

回答1:

Chrome rather strictly assumes that there is no connection between local files.

On a Mac you can start Chrome from the terminal like this:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files

and it will work, but the best thing would be to use a webserver.