这是我的代码 http://my-localhost.com/iframe-test.html
<html>
<head><title>Welcome Iframe Test</title></head>
<body>
<iframe src="http://www.my-website.com/index.html" width="500" height="500"></iframe>
<script type="text/javascript">
function alertMyMessage(msg){
alert(msg);
}
</script>
</body>
</html>
这是代码 http://www.my-website.com/index.html
<html>
<head></title>Welcome to my Server</title></head>
<body>
<h1>Welcome to My Server</ht>
<a href="javascript:void(0)" title="Click here" onClick="parent.alertMyMessage('Thanks for Helping me')">Click Here</a>
</body>
</html>
当我单击“单击此处”链接时。我得到以下错误。
未捕获到的SecurityError:阻止了源为“ http://www.my-website.com ”的框架访问源为“ http://my-localhost.com ”的框架。协议,域和端口必须匹配。
请帮助我解决此问题,或为此提供其他解决方案。
您无法访问在不同来源的框架中加载的页面的DOM。由于安全原因,该网站已被阻止(假设您访问了一个随机网站,并在隐藏的iframe中打开了Web邮件服务,您会发现原因)。
您可以使用的最接近的方法(只有同时控制两个网站)才可以使用网络消息传递api在它们之间传递消息。
在一页中,编写一个处理消息的函数,然后将其添加为消息事件侦听器。
function receiveMessage(event)
{
alert(event.data);
}
addEventListener("message", receiveMessage, false);
在另一个中,发送消息:
parent.postMessage("This is a message", "*");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句