iFrame 和 Javascript 到父文档
我有一个带有外部网站的 iFrame。我知道相同的域规则,因此我尝试通过 src 向父级调用一些 javascript。目前我有:
<iframe id="my_frame" src="http://other.com"></iframe>
并且我使用 javascript 更改了 src
,如下所示:
<iframe id="my_frame" src="javascript:document.write("blah");"></iframe>
但是使用 parent
不起作用:
<iframe id="my_frame" src="javascript:parent.document.write("blah");"></iframe>
相同的域规则是否也适用于父级,或者我在做什么有事吗?
I have an iFrame with an external website. I know about the same domain rule, so I am trying to invoke some javascript via the src
to the parent. Currently I have:
<iframe id="my_frame" src="http://other.com"></iframe>
and I change the src
using javascript as follows:
<iframe id="my_frame" src="javascript:document.write("blah");"></iframe>
but using the parent
does not work:
<iframe id="my_frame" src="javascript:parent.document.write("blah");"></iframe>
Does the same domain rule also apply for the parent or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这要么是一个出了问题的解决方法,要么是一个天才,但我不认识它:)你到底想实现什么目标?你想写入到子框架中,还是从子框架中写入到父文档中?
This is either a workaround gone awry, or it's genius and I don't recognize it :) What exactly are you trying to achieve? Do you want to write into the sub-frame, or from the sub-frame into the parent document?
我猜你的意图是向包含 iframe 的文档写入一些内容。
现在,首先,您的第一个代码片段无法工作,因为 javascript 中的双引号字符串本身位于属性值内,该属性值也用双引号引起来。它应该显示为:
这在您的最后一个片段中可能也是错误的,因此请进行相应的修复。
不过,我真的不明白你想要实现什么目标。愿意解释一下你为什么要尝试使用这个吗?
I am guessing your intention is to write something to the document containing the iframe.
now, first off, your first snippet can't work because the double-quoted string inside the javascript is itself inside an attribute value, which is also enclosed in double quotes. It should read:
This is probably also wrong in your last snippet, so fix accordingly.
Still, I don't really see what you are trying to achieve. Care to explain why you are trying to use this?