简体   繁体   English

iFrame和Javascript到父文档

[英]iFrame and Javascript to parent document

I have an iFrame with an external website. 我有一个带有外部网站的iFrame。 I know about the same domain rule, so I am trying to invoke some javascript via the src to the parent. 我知道相同的域规则,因此我尝试通过src调用一些javascript到父级。 Currently I have: 目前我有:

<iframe id="my_frame" src="http://other.com"></iframe>

and I change the src using javascript as follows: 并且我使用javascript更改了src ,如下所示:

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

but using the parent does not work: 但使用parent无效:

<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? 相同的域规则是否也适用于父级或我做错了什么?

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? 您要写入子框架,还是要从子框架写入父文档?

I am guessing your intention is to write something to the document containing the iframe. 我猜您的意图是向包含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. 现在,首先,您的第一个代码段无法正常工作,因为javascript内的双引号字符串本身位于属性值内,该属性值也用双引号引起来。 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? 想要解释一下您为什么要尝试使用此功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM