iFrame 和 Javascript 到父文档

发布于 2024-08-15 10:59:16 字数 520 浏览 1 评论 0原文

我有一个带有外部网站的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

草莓味的萝莉 2024-08-22 10:59:16

这要么是一个出了问题的解决方法,要么是一个天才,但我不认识它:)你到底想实现什么目标?你想写入到子框架中,还是从子框架中写入到父文档中?

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?

音栖息无 2024-08-22 10:59:16

我猜你的意图是向包含 iframe 的文档写入一些内容。

现在,首先,您的第一个代码片段无法工作,因为 javascript 中的双引号字符串本身位于属性值内,该属性值也用双引号引起来。它应该显示为:

<iframe id="my_frame" src="javascript:document.write('blah');"></iframe>

这在您的最后一个片段中可能也是错误的,因此请进行相应的修复。

不过,我真的不明白你想要实现什么目标。愿意解释一下你为什么要尝试使用这个吗?

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:

<iframe id="my_frame" src="javascript:document.write('blah');"></iframe>

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文