我有一个主页,用于导航到 iframe
中的其他 3 个页面
主页:1.html
其他3个页面:one.html、two.html、3.html。
代码
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>
一个.html
<html>
<body onload="try{alert(parent.location.href);} catch(e){alert(e);}">
1.html.
</body>
</html>
但是在显示 parent
文档的位置时出现问题。它给出如下警告消息:
SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
谁能解释一下在这个错误的上下文中消息中使用的术语的含义,或者简单地解释为什么会这样???
注意:未使用服务器
最佳答案
Chrome 相当严格地假设本地文件之间没有连接。
在 Mac 上,您可以像这样从终端启动 Chrome:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --allow-file-access-from-files
它会工作,但最好的办法是使用网络服务器。
关于javascript - 无法从 Iframe 访问父文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19462608/