【发布时间】:2013-03-15 11:18:05
【问题描述】:
首先,我知道这个问题与其他问题重复。但是我尝试从stackoverflow中的许多问题中跟踪许多样本,但它没有用。我无法从 iframe 调用主窗口的功能,但如果我使用框架标签,它就可以工作。
我试过这样。
主窗口:
<body>
<iframe src='test.html'></iframe>
<script>
var alertFunc = function(){
window.alert("work!");
}
</script>
</body>
test.html
<body>
<input type="button" onclick="window.parent.alertFunc();" value="Test" />
</body>
来自控制台的错误消息。
Unsafe JavaScript attempt to access frame with URL file:///C:/.../index.html from frame with URL file:///C:/.../test.html. Domains, protocols and ports must match.
test.html:30
Uncaught TypeError: Cannot call method 'alertFunc' of undefined
还有很多类似的形式。
你有什么想法吗?还是我做错了什么?
【问题讨论】:
-
检查您的浏览器控制台是否有任何错误
-
@Arun P Johny 错误已更新
-
这是因为same origin policy,如果您将站点部署在tomcat或任何其他Web服务器中,而不是从本地文件系统访问它应该可以工作
标签: javascript html function iframe frame